【问题标题】:I want to retain a bitmap when changing orientation of screen我想在更改屏幕方向时保留位图
【发布时间】:2019-03-02 14:40:00
【问题描述】:

我制作了一个自定义的 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);

注意:我为两个不同的方向使用了两个布局文件,并希望保持这种方式。所以不要手动处理配置更改。

【问题讨论】:

    标签: android android-activity bitmap retain


    【解决方案1】:

    当屏幕方向改变时,Android 会删除整个 Activity 并重新启动它,因此您需要在发生这种情况时保存更改的位图并在之后恢复它。

    描述了不同的存储选项here

    描述了何时保存here

    【讨论】:

    • 您好,感谢您的回复,但我已经尝试将位图保存到文件中,但这需要很长时间。
    • 您还可以在用户制作时保存每个笔触,并在重新启动活动时重播所有笔触。
    • 那将是一个解决方案是的。你能指导我一些补充代码或文档吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多