android中Bitmap的放大和缩小的方法

/**Bitmap放大的方法*/

private static Bitmap big(Bitmap bitmap) {
  Matrix matrix = new Matrix();
  matrix.postScale(1.5f,1.5f); //长和宽放大缩小的比例
  Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
  return resizeBmp;
 }

/**Bitmap缩小的方法*/

 private static Bitmap small(Bitmap bitmap) {
  Matrix matrix = new Matrix();
  matrix.postScale(0.8f,0.8f); //长和宽放大缩小的比例
  Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
  return resizeBmp;
 }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android的触摸放大缩小功能可以通过实现GestureDetector类来实现。以下是一个简单的示例代码: ```java public class MyView extends View { private Paint paint; private Bitmap bitmap; private float scaleFactor = 1.0f; private GestureDetector gestureDetector; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); gestureDetector = new GestureDetector(getContext(), new MyGestureListener()); } @Override public boolean onTouchEvent(MotionEvent event) { gestureDetector.onTouchEvent(event); return true; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.save(); canvas.scale(scaleFactor, scaleFactor); canvas.drawBitmap(bitmap, 0, 0, paint); canvas.restore(); } private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { scaleFactor *= detector.getScaleFactor(); scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f)); invalidate(); return true; } } } ``` 在这个示例,我们使用了GestureDetector和ScaleGestureDetector来实现放大缩小功能。我们在MyView类重写了onTouchEvent()方法来接收触摸事件,然后将事件传递给gestureDetector处理。在MyGestureListener类,我们重写了onScale()方法,该方法在用户进行放大缩小手势时被调用,我们在此方法更新scaleFactor变量,然后调用invalidate()方法强制视图重新绘制。最后,在onDraw()方法,我们调用canvas.scale()方法来缩放画布,然后绘制位图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值