android - 我想在更改屏幕方向时保留位图
问题描述
我制作了一个自定义的 ImageView,它有一个画布并获得了两个位图,这样我就可以在现有图像上进行绘制。虽然方向改变了图像是可见的,但我画的线条消失了。
public class DrawableImageView extends ImageView implements View.OnTouchListener, DrawableImageViewControlListener {
float downx = 0;
float downy = 0;
float upx = 0;
float upy = 0;
Canvas canvas;
Paint paint;
Matrix matrix;
boolean isChanged;
public DrawableImageView(Context context) {
super(context);
setOnTouchListener(this);
}
public DrawableImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setOnTouchListener(this);
}
public DrawableImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnTouchListener(this);
}
public void setNewImage(Bitmap alteredBitmap, Bitmap bmp)
{
canvas = new Canvas(alteredBitmap);
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
paint.setStrokeCap(Paint.Cap.ROUND);
matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
isChanged = false;
setImageBitmap(alteredBitmap);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
isChanged = true;
switch (action)
{
case MotionEvent.ACTION_DOWN:
downx = getPointerCoords(event)[0];//event.getX();
downy = getPointerCoords(event)[1];//event.getY();
break;
case MotionEvent.ACTION_MOVE:
upx = getPointerCoords(event)[0];//event.getX();
upy = getPointerCoords(event)[1];//event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
invalidate();
downx = upx;
downy = upy;
break;
case MotionEvent.ACTION_UP:
upx = getPointerCoords(event)[0];//event.getX();
upy = getPointerCoords(event)[1];//event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
final float[] getPointerCoords(MotionEvent e)
{
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix matrix = new Matrix();
getImageMatrix().invert(matrix);
matrix.postTranslate(getScrollX(), getScrollY());
matrix.mapPoints(coords);
return coords;
}
@Override
public void colorChanged(int color) {
paint.setColor(color);
}
@Override
public void brushChanged(int size) {
paint.setStrokeWidth(size);
}
public boolean isChanged() {
return isChanged;
}
这是我在 OnCreate 中初始化 ImageView 的代码:
imageView = (DrawableImageView) findViewById(R.id.editImageView);
IStorage storage = BootLoader.resolve(this).getStorage();
imageFile = storage.getImageFile(fileName);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
if (alteredBitmap == null) {
alteredBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
}
imageView.setNewImage(alteredBitmap, bitmap);
注意:我为两个不同的方向使用了两个布局文件,并希望保持这种方式。所以不要手动处理配置更改。
解决方案
推荐阅读
- reactjs - 失败的道具类型:无效的道具值`[object Object],[object Object]`,应为[null,null]之一
- python-3.x - 在 ubuntu 18.04.4 上运行 sudo dpkg --configure -a 命令时 python3 django 出错
- html - 超过 1024 个字符的字符串上的 PosEx
- javascript - 从另一个组件到达组件内声明的对象 - React Native
- django - django.shortcuts 和 rest_framework.generics 之间的区别
- flutter - 无法从 ListView.builder() 中弹出
- c# - Action 有多个参数被指定或推断为从请求正文绑定。每个动作只能绑定一个参数
- ios - 如果 info.plist 不包含某些键,如何避免应用程序崩溃?
- argocd - 将 TLA 从 argocd 传递到 jsonnet
- reactjs - Reactjs:我必须刷新登录组件才能将经过身份验证的用户重定向到仪表板