简体   繁体   English

图像两边的白色边框

[英]White border on both sides of the image

I'm trying to load my my image to a custom view but after loading im getting withite border on both sides. 我正在尝试将我的图片加载到自定义视图,但是加载后我的两侧都出现了边框。

在此处输入图片说明

My custom imageView 我的自定义imageView

MyCanvas 我的画布

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);
}

Over here im trying to draw on image with different colors. 我在这里尝试用不同的颜色绘制图像。

my method which loads image to MyCanvas imageview 我的方法将图像加载到MyCanvas imageview

private void setPreviewImage(final Bitmap bmp) {
    if (bmp != null) {
        mCanvas.setImageBitmap(bmp);
    }
}

The xml 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"/>

It is not exactly a white border, the image you are setting in your custom view is not of exactly the same size as your MyCanvas view. 它并非完全是白色边框,您在自定义视图中设置的图像与MyCanvas视图的尺寸并不完全相同。 You will need to scale or crop your image if you want it to take up all the space without leaving any empty spaces around. 如果希望图像占据所有空间而不在周围留有空白,则需要缩放或裁剪图像。

Try adding android:scaleType="fitXY" to your MyCanvas in your layout.xml. 尝试在android:scaleType="fitXY"MyCanvas中。 Your MyCanvas would look like: 您的MyCanvas看起来像:

<a9msquare.mustache.Views.MyCanvas
    android:id="@+id/my_canvas"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM