- package com.example.aimage;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Path.Direction;
- import android.graphics.PorterDuff.Mode;
- import android.graphics.PorterDuffXfermode;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- private ImageView mImageView1;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mImageView1 = (ImageView) findViewById(R.id.imageView1);
- mImageView1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View paramView) {
- mImageView1.setImageBitmap(getBitmap());
- }
- });
- }
- private Bitmap getBitmap() {
- Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.earth);
- Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888);
- drawDestBitmap(src, dest);
-
- return dest;
- }
-
- private void drawDestBitmap(Bitmap src, Bitmap dest) {
- Canvas canvas_d = new Canvas(dest);
- Paint paint_d = new Paint();
- Bitmap mask = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ALPHA_8);
- long begin = System.currentTimeMillis();
-
- drawMaskBitmap(mask);
- canvas_d.drawBitmap(mask, 0, 0, paint_d);
- paint_d.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
- canvas_d.drawBitmap(src, 0, 0, paint_d);
- long count = System.currentTimeMillis() - begin;
- Log.d("", " :" + count);
- }
-
- private void drawMaskBitmap(Bitmap mask) {
- Canvas canvas_m = new Canvas(mask);
- Paint paint_m = new Paint();
- Path path = new Path();
- path.addCircle(500, 400, 270, Direction.CCW);
- paint_m.setAntiAlias(true);
- paint_m.setAlpha(0xff);
- canvas_m.drawPath(path, paint_m);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
复制代码 |
|