android - 在android中绘制 ImageView

标签 android

我尝试保存 ImageView 的光栅,但是当我获取光栅时,背景颜色变为黑色。我不太确定发生了什么,但我将背景删除为白色,但每次将其保存到 SQLite 并再次获取和设置时,它都会变为黑色。 这是我的服装抽屉类。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyDrawView extends View
{
public Bitmap mBitmap;
public Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;

public MyDrawView(Context c, AttributeSet attrs)
{
    super(c, attrs);

    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.WHITE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(9);

}

public void eraser()
{
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.WHITE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(22);
}

public void pen(int color, int size)
{
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(color);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(size);
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
    super.onSizeChanged(w, h, oldw, oldh);
    mBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas)
{
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);


}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y)
{
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touch_move(float x, float y)
{
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
    {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touch_up()
{
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
}

public Bitmap getBitmap()
{
    //this.measure(100, 100);
    //this.layout(0, 0, 100, 100);
    this.setDrawingCacheEnabled(true);
    this.buildDrawingCache();
    Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());
    this.setDrawingCacheEnabled(false);


    return bmp;
}

public void clear()
{
    mBitmap.eraseColor(Color.WHITE);
    invalidate();
    System.gc();

}

}

在我的 Activity 中,我有以下代码: 公共(public)类 MainActivity 扩展 AppCompatActivity { 我的绘图 View ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = findViewById(R.id.img);
    myDrawView = findViewById(R.id.draw);


 myDrawView.setBackground(getResources()
   .getDrawable(R.drawable.bg_customer));
    Button button1 = findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Bitmap well = myDrawView.getBitmap();
            imageView.setImageBitmap(well);
            imageView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    //now save in room database 
                }
            });
        }
    });
}

}

当我保存时我的绘画背景是黑色的(不是图像) 谁能帮帮我?

最佳答案

使用dispatchDraw(Canvas canvas)方法进行绘制。

关于android - 在android中绘制 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55353983/

相关文章:

javascript - 为什么setSelectionRange不能在android 2.3平台浏览器上工作

android - 5 毫秒后调用方法

安卓谷歌 IO 2014 : How to implement Activity transition with fragments?

java - Android 使用 ftp api 保存到内存

android - CSS 背景图像在 Android 手机上无法正常工作

android - 从一个 Activity 切换到另一个 Activity 时如何在两个 Activity 中显示进度对话框

android - JSON 解析器性能低下

android - 如何将项目从android studio迁移到android studio

android - 我可以排除将资源值构建到我的 Android 子项目中吗?

android - 插件在 android phonegap 中不起作用