android 手势移动bitmap
时间: 2023-10-30 13:35:45 浏览: 33
实现的方式有哪些?
可以通过以下方式实现:
1. 使用Matrix类的setTranslate()方法来实现位图的移动。
2. 使用Canvas类的drawBitmap()方法并在参数中指定位图的坐标来实现位图的移动。
3. 使用SurfaceView类的双缓冲机制并在SurfaceHolder类的回调函数中实现位图的移动。
4. 使用OpenGL ES库中提供的纹理映射功能来实现位图的移动。
相关问题
android 画布移动bitmap
在 Android 中,可以使用 Canvas 和 Bitmap 对象来实现画布移动。具体步骤如下:
1. 创建一个新的 Bitmap 对象,用于存储要绘制的图像。
2. 创建一个 Canvas 对象,将 Bitmap 对象设置为画布。
3. 在 Canvas 上使用 drawBitmap 方法绘制图像,可以使用 Matrix 对象来控制图像的移动。
4. 使用 Canvas 的 translate 方法来移动画布。
5. 调用 invalidate 方法使 View 无效,强制重新绘制。
下面是一个示例代码:
```java
private Bitmap bitmap;
private Canvas canvas;
private Matrix matrix = new Matrix();
private float translateX, translateY;
// 在构造函数或 onSizeChanged 中创建 Bitmap 和 Canvas 对象
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 移动画布
canvas.translate(translateX, translateY);
// 绘制 Bitmap
canvas.drawBitmap(bitmap, matrix, null);
// 使 View 无效,强制重新绘制
invalidate();
}
```
可以通过修改 translateX 和 translateY 变量的值来控制画布的移动。
android rgb转bitmap
下面是一段将 RGB 转换为 Bitmap 的示例代码:
```java
public Bitmap rgbToBitmap(int[] pixels, int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
```
其中,pixels 是一个 int 数组,表示每个像素点的 RGB 值,width 和 height 分别表示图片的宽度和高度。这段代码将 RGB 值转换为 ARGB 值(因为 Android 中的 Bitmap 类型是 ARGB),并将其设置为 Bitmap 的像素点,最后返回生成的 Bitmap 对象。