i3geek.com
闫庚哲的个人博客

Android 在ImageView 上画个正方形

public void first()
{
  // 防止出现Immutable bitmap passed to Canvas constructor错误
  Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.c0101a).copy(Bitmap.Config.ARGB_8888, true);
  //Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.drawable.go)).getBitmap();
  Bitmap newBitmap = null;
  newBitmap = Bitmap.createBitmap(bitmap1);
  Canvas canvas = new Canvas(newBitmap);
  Paint paint = new Paint();
  int w = bitmap1.getWidth();
  int h = bitmap1.getHeight();
//  int w_2 = bitmap2.getWidth();
//  int h_2 = bitmap2.getHeight();

  paint.setAntiAlias(true);//设置画笔无锯齿(如果不设置可以看到效果很差)
  /*设置paint的 style 为STROKE:空心*/
        paint.setStyle(Paint.Style.STROKE);
  paint.setColor(Color.RED);
  paint.setAlpha(125);
  canvas.drawRect(100, 100, 50, 50, paint);
  //paint = new Paint();
  //canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2, Math.abs(h - h_2) / 2, paint);
  canvas.save(Canvas.ALL_SAVE_FLAG);
  // 存储新合成的图片
  canvas.restore();
  find_img1.setImageBitmap(newBitmap);
}

 

赞(0)
未经允许不得转载:爱上极客 » Android 在ImageView 上画个正方形
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址