影象處理詳解之影象透明度
阿新 • • 發佈:2019-01-11
Android支援的顏色系統是RGB,Alpha最後的Alpha表示透明度,他的取值是0-255,。通過設定Alpha的值,我們可以改變影象的透明度。
下面舉例說明,效果圖如下:
public class MainActivity extends Activity implements OnSeekBarChangeListener{ public static int alpha=100; private View myView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout=new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); myView=new MyView(this); myView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 260)); SeekBar seekBar=new SeekBar(this); seekBar.setMax(255); //seekbar的初始值 seekBar.setProgress(alpha); seekBar.setOnSeekBarChangeListener(this); linearLayout.addView(myView); linearLayout.addView(seekBar); linearLayout.setBackgroundColor(color.white); setContentView(linearLayout); setTitle("Alpha"+alpha); } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub alpha=progress; setTitle("Alpha:"+alpha); myView.invalidate(); } private class MyView extends View{ private Bitmap bitmap; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub InputStream is=getResources().openRawResource(R.drawable.ic_launcher); bitmap=BitmapFactory.decodeStream(is); setBackgroundColor(color.white); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub Paint paint=new Paint(); paint.setAlpha(alpha); //第二個引數表示源點陣圖的複製區域,第三個引數表示繪製的目標區域 canvas.drawBitmap(bitmap, new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),new Rect(10,10,310,235), paint); } } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } }