请问 在做橡皮擦的时候为什么PorterDuff.Mode.SRC_OVER和PorterDuff.Mode.SRC_IN不一样

「已注销」 2016-05-25 06:05:14

为什么橡皮擦擦出的时候会在轮廓外面套一个长方形啊


自定义的VIew

package com.hengshu.ban;
//javaapk.com 安卓源码服务专家提供下载
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View
{

Paint mPaint = null; // used to draw line
Paint mBitmapPaint = null; // used to draw bitmap
Path mPath = null; // save the point
Bitmap mBitmap = null; // used as choosing picture
Bitmap mBottomBitmap = null;// used as bottom background
Canvas mCanvas = null; // what's it used for
float posX,posY; // used as touched position
private final float TOUCH_TOLERANCE = 2;

private DrawPath mDrawPath = null;
private List<DrawPath> mSavePath = null;
private List<DrawPath> mDeletePath = null;
private String mImagePath = null;

private int mImageWidth = 480;
private int mImageHeight = 800;
private int mBottomBitmapDrawHeight = 0;

public MyView(Context context,AttributeSet attr,int defStyle)
{
super(context,attr,defStyle);
init();
}
public MyView(Context context,AttributeSet attr)
{
super(context,attr);
init();
}

public MyView(Context context)
{
super(context);
init();
}

private void init()
{
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);

mBitmapPaint = new Paint(Paint.DITHER_FLAG);

mSavePath = new ArrayList<DrawPath>();
mDeletePath = new ArrayList<DrawPath>();
mImagePath = initPath();
}

private String initPath()
{
String ph = Environment.getExternalStorageDirectory().getAbsolutePath();
if(ph == null)
{
return null;
}
ph += "/ddxxtuya";
File imageFile = new File(ph);
if( !imageFile.exists() )
{
imageFile.mkdir();
}
return ph;
}

private class DrawPath
{
Path path;
Paint paint;
}

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

@Override
public void onDraw(Canvas canvas) {

canvas.drawColor(Color.WHITE);
canvas.getWidth();
int nCanvasHeight = canvas.getHeight();
mBottomBitmap.getWidth();
int nBitmapHeight = mBottomBitmap.getHeight();
mBottomBitmapDrawHeight = (nCanvasHeight - nBitmapHeight)/2;
canvas.drawBitmap(mBottomBitmap,0,mBottomBitmapDrawHeight,mBitmapPaint);
if(mPath != null)
{
canvas.drawPath(mPath, mPaint);
}


}

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

switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
mPath = new Path();
mDrawPath = new DrawPath();
mPath.moveTo(x, y);
mDrawPath.paint = new Paint(mPaint);
mDrawPath.path = mPath;
posX = x;
posY = y;
//postInvalidate();
Log.i("--li--", "ACTION_DOWN");

break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(x - posX);
float dy = Math.abs(y - posY);
if(dx >= TOUCH_TOLERANCE || dy > TOUCH_TOLERANCE)
{
mPath.quadTo(posX, posY, (x + posX)/2, (y + posY)/2);
posX = x;
posY = y;
}

Log.i("--li--", "ACTION_MOVE");
//postInvalidate();
break;
case MotionEvent.ACTION_UP:
mPath.lineTo(posX, posY);
mPath.offset(0, -mBottomBitmapDrawHeight);

mCanvas.drawPath(mPath,mPaint);
mSavePath.add(mDrawPath);
mPath = null;
// postInvalidate();
Log.i("--li--", "ACTION_UP");
break;
}
postInvalidate();
return true;
}

public boolean setBitmap(String imagePath)
{
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float nxScale = -1;
float nyScale = -1;
if( width!=0 && height!=0)
{
nxScale = (float)width/mImageWidth;
nyScale = (float)height/mImageHeight;
if (nxScale>=1 && nyScale >=1 || nxScale<1 && nyScale<1)
{
if(nxScale > nyScale)
{
width = (int)(width/nxScale);
height = (int)(height/nxScale);
}
else
{
width = (int)(width/nyScale);
height = (int)(height/nyScale);
}

}
if (nxScale >=1 && nyScale <1)
{
width = mImageWidth;
}
if(nxScale <=1 && nyScale >=1)
{
height = mImageHeight;
}
mBitmap = Bitmap.createScaledBitmap(bitmap,width,height,true);
mBottomBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
mSavePath.clear();
mDeletePath.clear();
mCanvas.setBitmap(mBottomBitmap);
mCanvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
postInvalidate();

return true;
}
else
return false;

}

public void setBitmapColor(int color)
{
mBottomBitmap.eraseColor(color);
mSavePath.clear();
mDeletePath.clear();
postInvalidate();
}

public void setPaint(Paint paint)
{
mPaint = paint;
postInvalidate();
}
//保存成图片
public void saveImage(String imagePath)
{
if (mImagePath == null || mBitmap == null)
{
return;
}
String imageName = null;
int nStart = imagePath.lastIndexOf('/');
int nEnd = imagePath.lastIndexOf('.');

imageName = imagePath.substring(nStart,nEnd);
imageName += ".png";
imageName = mImagePath + imageName;
File file = new File(imageName);

try {
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
mBottomBitmap.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}


//清楚全部
public void clearImage()
{
mSavePath.clear();
mDeletePath.clear();

if(mBitmap != null)
{
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
mBottomBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBottomBitmap);
mCanvas.drawBitmap(mBitmap, 0,0, mBitmapPaint);
}
else
{
int width = mCanvas.getWidth();
int height = mCanvas.getHeight();
mBottomBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBottomBitmap);

}
postInvalidate();

}
//取消
public void undo()
{
int nSize = mSavePath.size();
if (nSize >= 1)
{
mDeletePath.add(0, mSavePath.get(nSize-1) );
mSavePath.remove(nSize -1);
}
else
return;


if(mBitmap != null)
{
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
mBottomBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBottomBitmap);
mCanvas.drawBitmap(mBitmap, 0,0, mBitmapPaint);
}
else
{
int width = mCanvas.getWidth();
int height = mCanvas.getHeight();
mBottomBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBottomBitmap);
}

Iterator<DrawPath> iter = mSavePath.iterator();
DrawPath temp;
while(iter.hasNext())
{
temp = iter.next();
mCanvas.drawPath(temp.path, temp.paint);
}
postInvalidate();

}
//重画
public void redo()
{
int nSize = mDeletePath.size();
if (nSize >= 1)
{
mSavePath.add( mDeletePath.get(0) );
mDeletePath.remove(0);
}
else
return;


if(mBitmap != null)
{
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
mBottomBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBottomBitmap);
mCanvas.drawBitmap(mBitmap, 0,0, mBitmapPaint);
}
else
{
int width = mCanvas.getWidth();
int height = mCanvas.getHeight();
mBottomBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBottomBitmap);
}

Iterator<DrawPath> iter = mSavePath.iterator();
DrawPath temp;
while(iter.hasNext())
{
temp = iter.next();
mCanvas.drawPath(temp.path, temp.paint);
}
postInvalidate();
}
}


...全文
192 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2016-08-08
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶顶

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧