Android视图中的橡皮擦

Android视图中的橡皮擦,第1张

概述我正在创建一个在画布上绘制的应用程序.要绘制我将笔颜色设置为黑色. 用于笔按钮 int black = Color.BLACK; mDrawPaint = new DrawPaint(Capture.this, null,black); DrawPaint扩展View的位置 现在创建橡皮擦我刚刚将笔颜色改为白色,这是画布的背景颜色.像这样 用于橡皮擦按钮 int white = Color.WH 我正在创建一个在画布上绘制的应用程序.要绘制我将笔颜色设置为黑色.

用于笔按钮

int black = color.BLACK; mDrawPaint = new DrawPaint(Capture.this,null,black);

DrawPaint扩展VIEw的位置

现在创建橡皮擦我刚刚将笔颜色改为白色,这是画布的背景颜色.像这样

用于橡皮擦按钮

int white = color.WHITE; mDrawPaint = new DrawPaint(Capture.this,white);

但是,如果我选择笔颜色为黑色并在画布上绘制某些东西的笔按钮,它会再次自动重绘之前我在绘制之前绘制的颜色.橡皮擦也擦除了一个大的矩形区域.请解释我出了什么问题.谢谢.

这是DrawPaint构造函数

public DrawPaint(Context context,AttributeSet attrs,int color) {    super(context,attrs);    this.destroyDrawingCache();    paint.setAntiAlias(true);    paint.setcolor(color);    paint.setStyle(Paint.Style.stroke);    paint.setstrokeJoin(Paint.Join.ROUND);    paint.setstrokeWIDth(stroke_WIDTH);}    public boolean ontouchEvent(MotionEvent event) {        eventX = event.getX();        eventY = event.getY();        button1.setEnabled(true);        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:            path.moveto(eventX,eventY);            lasttouchX = eventX;            lasttouchY = eventY;            return true;        case MotionEvent.ACTION_MOVE:        case MotionEvent.ACTION_UP:            resetDirtyRect(eventX,eventY);            int historySize = event.getHistorySize();            for (int i = 0; i < historySize; i++) {                float historicalX = event.getHistoricalX(i);                float historicalY = event.getHistoricalY(i);                expandDirtyRect(historicalX,historicalY);                path.lineto(historicalX,historicalY);            }            path.lineto(eventX,eventY);            break;        default:            deBUG("Ignored touch event: " + event.toString());            return false;        }        invalIDate((int) (dirtyRect.left - HALF_stroke_WIDTH),(int) (dirtyRect.top - HALF_stroke_WIDTH),(int) (dirtyRect.right + HALF_stroke_WIDTH),(int) (dirtyRect.bottom + HALF_stroke_WIDTH));        lasttouchX = eventX;        lasttouchY = eventY;        return true;    }
解决方法 对于橡皮擦,您可以使用此代码…

mPaint.setMaskFilter(null);mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

关于那个方形颜色问题……

看一下这个..
这是画布绘制的最佳示例,具有擦除,模糊和浮雕效果……

public class FingerText extends Activity    implements colorPickerDialog.OncolorChangedListener {    @OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(new MyVIEw(this));    mPaint = new Paint();    mPaint.setAntiAlias(true);    mPaint.setDither(true);    //this is the line that sets the initial pen color    mPaint.setcolor(inkcolor);    mPaint.setStyle(Paint.Style.stroke);    mPaint.setstrokeJoin(Paint.Join.ROUND);    mPaint.setstrokeCap(Paint.Cap.ROUND);    mPaint.setstrokeWIDth(2);}private Paint       mPaint;private Bitmap      mBitmap;private boolean             inkChosen;private int bgcolor = 0xFFFFFFFF;  //set initial bg color var to whiteprivate int inkcolor =  0xFF000000; //set initial ink color var to blackpublic voID colorChanged(int color) {    //This is the implementation of the interface from colorpickerdialog.java    if (inkChosen){            mPaint.setcolor(color);            inkcolor = color;    }    else {            mBitmap.erasecolor  (color);            bgcolor = color;            //set the color to the user's last ink color choice            mPaint.setcolor(inkcolor);     }}public class MyVIEw extends VIEw {    private Canvas  mCanvas;    private Path    mPath;    private Paint   mBitmapPaint;    public MyVIEw(Context c) {        super(c);        mBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);        //this sets the bg color for the bitmap        mBitmap.erasecolor  (bgcolor);        mCanvas = new Canvas(mBitmap);        mPath = new Path();        mBitmapPaint = new Paint(Paint.DITHER_FLAG);    }    @OverrIDe    protected voID onSizeChanged(int w,int h,int olDW,int oldh) {        super.onSizeChanged(w,h,olDW,oldh);    }    @OverrIDe    protected voID onDraw(Canvas canvas) {        //this is the line that changes the bg color in the initial canvas            canvas.drawcolor(bgcolor);        canvas.drawBitmap(mBitmap,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;    }}private static final int BG_color_ID = Menu.FirsT;private static final int INK_MENU_ID = Menu.FirsT + 1;private static final int CLEAR_MENU_ID = Menu.FirsT + 2;private static final int ERASER_MENU_ID = Menu.FirsT + 3;private static final int SEND_MENU_ID = Menu.FirsT + 4;@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    super.onCreateOptionsMenu(menu);    menu.add(0,BG_color_ID,"Background color").setShortcut('3','b');    menu.add(0,INK_MENU_ID,"Ink color").setShortcut('4','c');    menu.add(0,CLEAR_MENU_ID,"Clear All").setShortcut('5','e');    menu.add(0,ERASER_MENU_ID,"Eraser").setShortcut('6','x');    menu.add(0,SEND_MENU_ID,"Send").setShortcut('7','s');    /****   Is this the mechanism to extend with filter effects?    Intent intent = new Intent(null,getIntent().getData());    intent.addcategory(Intent.category_ALTERNATIVE);    menu.addIntentoptions(                          Menu.ALTERNATIVE,new Componentname(this,NotesList.class),intent,null);    *****/    return true;}@OverrIDepublic boolean onPrepareOptionsMenu(Menu menu) {    super.onPrepareOptionsMenu(menu);    return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) {    mPaint.setXfermode(null);    mPaint.setAlpha(0xFF);    switch (item.getItemID()) {            case BG_color_ID:                    new colorPickerDialog(this,this,mPaint.getcolor()).show();                    inkChosen = false;                    return true;        case INK_MENU_ID:            new colorPickerDialog(this,mPaint.getcolor()).show();            //remember the user's last ink choice color so we can revert after eraser            //to background color change -- otherwise ink is last bg color            inkcolor = mPaint.getcolor();            inkChosen = true;            return true;        case CLEAR_MENU_ID:            mBitmap.erasecolor  (bgcolor);            return true;        case ERASER_MENU_ID:            //set pen color to bg color for 'erasing'            mPaint.setcolor(bgcolor);            return true;        case SEND_MENU_ID:                    /* Todo need to decIDe whether to save image locally                     * and how to make it available if so. really only need to                     * save if we want to let users vIEw later,or pick a                     * prevIoUs message to send again                      */                    // this try-catch block creates a private file and an                    // inputstream pointing to it for reading            fileinputStream ifs;                            try {                                    fileOutputStream fs = openfileOutput("message_image",Context.MODE_PRIVATE);                                    mBitmap.compress(CompressFormat.PNG,100,fs);                                    ifs = openfileinput("message_image");                            } catch (fileNotFoundException e) {                                    // Todo auto-generated catch block                                    e.printstacktrace();                                    return true;                            }                                    // inserts file pointed to by ifs into image gallery                            String url = Images.Media.insertimage(getContentResolver(),BitmapFactory.decodeStream(ifs),"Message image1","Message image");                                    // alternative: inserts mBitmap into image gallery/*              String url = Images.Media.insertimage(getContentResolver(),mBitmap,"Message image");*/                                    // creates the Intent to open the messaging app                                    // with the image at url attached            Intent sendIntent = new Intent(Intent.ACTION_SEND);             sendIntent.putExtra("sms_body","Message created using FingerText");             sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(url));            sendIntent.setType("image/png");            startActivity(sendIntent);                    /* Todo delete the image from the content provIDer                     * following line deletes the image,but too soon!                     *///              getContentResolver().delete(Uri.parse(url),null);            //this resets canvas after send               //Could also reset to last user settings w/o var resets            bgcolor = 0xFFFFFFFF;  //set bg color var back to white            inkcolor =  0xFF000000; //set ink color var back to black            mBitmap.erasecolor  (bgcolor);            return true;    }    return super.onoptionsItemSelected(item);}

}

总结

以上是内存溢出为你收集整理的Android视图中的橡皮擦全部内容,希望文章能够帮你解决Android视图中的橡皮擦所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1127222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存