我正在尝试将我的图片加载到自定义视图中,但在加载后我会在两边都加上边框。
我的自定义imageView
public class MyCanvas extends ImageView {
private Paint mPaint;
private Path mPath;
private Map<Path, Integer> mPaths;
private PathsChangedListener mListener;
private int mColor;
private float mCurX;
private float mCurY;
private float mStartX;
private float mStartY;
private boolean mPaintIsOn;
private Activity mScaleDetector;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private float mPosX;
private float mPosY;
public void setmPaintIsOn(boolean mPaintIsOn) {
this.mPaintIsOn = mPaintIsOn;
}
public MyCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5f);
mPaint.setAntiAlias(true);
mPaths = new LinkedHashMap<>();
mPaths.put(mPath, mPaint.getColor());
pathsUpdated();
}
public void setListener(PathsChangedListener listener) {
this.mListener = listener;
}
public void undo() {
if (mPaths.size() <= 0)
return;
Path lastKey = null;
for (Path key : mPaths.keySet()) {
lastKey = key;
}
mPaths.remove(lastKey);
pathsUpdated();
invalidate();
}
public void setColor(int newColor) {
mColor = newColor;
}
public Bitmap getBitmap() {
final Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
draw(canvas);
return bitmap;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mPaintIsOn) {
for (Map.Entry<Path, Integer> entry : mPaths.entrySet()) {
mPaint.setColor(entry.getValue());
canvas.drawPath(entry.getKey(), mPaint);
}
mPaint.setColor(mColor);
canvas.drawPath(mPath, mPaint);
}
}
public void clearCanvas(){
mPath.reset();
mPaths.clear();
pathsUpdated();
invalidate();
}
private void actionDown(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mCurX = x;
mCurY = y;
}
private void actionMove(float x, float y) {
mPath.quadTo(mCurX, mCurY, (x + mCurX) / 2, (y + mCurY) / 2);
mCurX = x;
mCurY = y;
}
private void actionUp() {
mPath.lineTo(mCurX, mCurY);
// draw a dot on click
if (mStartX == mCurX && mStartY == mCurY) {
mPath.lineTo(mCurX, mCurY + 2);
mPath.lineTo(mCurX + 1, mCurY + 2);
mPath.lineTo(mCurX + 1, mCurY);
}
mPaths.put(mPath, mPaint.getColor());
pathsUpdated();
mPath = new Path();
}
private void pathsUpdated() {
if (mListener != null && mPaths != null) {
mListener.pathsChanged(mPaths.size());
}
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
final MotionEvent event = ev;
if (mPaintIsOn) {
final float x = ev.getX();
final float y = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartX = x;
mStartY = y;
actionDown(x, y);
break;
case MotionEvent.ACTION_MOVE:
actionMove(x, y);
break;
case MotionEvent.ACTION_UP:
actionUp();
break;
default:
break;
}
invalidate();
return true;
}
return true;
}
public interface PathsChangedListener {
void pathsChanged(int cnt);
}
在这里,我试图在不同颜色的图像上绘图。
我的方法将图像加载到MyCanvas imageview
private void setPreviewImage(final Bitmap bmp) {
if (bmp != null) {
mCanvas.setImageBitmap(bmp);
}
}
xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<a9msquare.mustache.Views.MyCanvas
android:id="@+id/my_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/color_picker"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_alignParentRight="true"/>
<ImageView
android:id="@+id/undo"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_alignParentRight="true"
android:layout_below="@id/color_picker"
android:src="@mipmap/ic_launcher"
android:visibility="gone"/>
答案 0 :(得分:2)
它不完全是白色边框,您在自定义视图中设置的图像与MyCanvas
视图的尺寸不完全相同。如果您希望它占据所有空间而不留下任何空白区域,则需要缩放或裁剪图像。
尝试将android:scaleType="fitXY"
添加到layout.xml中的MyCanvas
。您的MyCanvas
看起来像是:
<a9msquare.mustache.Views.MyCanvas
android:id="@+id/my_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />