问题描述
- 为什么我写的android调色板小程序只能调单一颜色深浅,无法混合调色?
-
package com.example.palettebyandroid;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity {
private SeekBar sb_red; private SeekBar sb_green; private SeekBar sb_blue; private Bitmap bitmap; private Bitmap newBitmap; private Canvas canvas; private ColorMatrix cm; private Paint paint; private SeekBar sb_transparent; private ImageView iv_image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_image = (ImageView) findViewById(R.id.iv_image); sb_red = (SeekBar) findViewById(R.id.sb_red); sb_green = (SeekBar) findViewById(R.id.sb_green); sb_blue = (SeekBar) findViewById(R.id.sb_blue); sb_transparent = (SeekBar) findViewById(R.id.sb_transparent); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.after8); newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); canvas = new Canvas(newBitmap); paint =new Paint(); cm = new ColorMatrix(); cm.set(new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, new Matrix(), paint); sb_red.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); float fred = progress/128.0f; cm.set(new float[] { fred, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, new Matrix(), paint); iv_image.setImageBitmap(newBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); sb_green.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); float fgreen = progress/128.0f; cm.set(new float[] { 1, 0, 0, 0, 0, 0, fgreen, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, new Matrix(), paint); iv_image.setImageBitmap(newBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); sb_blue.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); float fblue = progress/128.0f; cm.set(new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, fblue, 0, 0, 0, 0, 0, 1, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, new Matrix(), paint); iv_image.setImageBitmap(newBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); sb_transparent.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); float ftran = progress/128.0f; cm.set(new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, ftran, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, new Matrix(), paint); iv_image.setImageBitmap(newBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); } }
解决方案
说实话,你的代码写得挺乱的。建议你参考这篇文章整理一下代码
http://www.devstore.cn/essay/essayInfo/1570.html
解决方案二:
说实话,你的代码写得挺乱的。建议你参考这篇文章整理一下代码
http://www.devstore.cn/essay/essayInfo/1570.html
时间: 2024-12-04 17:14:27