自定义ImageView适合其父宽度和高度

时间:2014-04-14 10:23:46

标签: android image image-processing android-imageview

我一直在创建图像处理应用程序。图像变黑的地方(技术上在paint上使用pathTouchEvent在画布上绘图。

这是我用于Blackened Screen的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/blacken_imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
     />
</FrameLayout>

在Blacken活动中:

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.drawingpad_layout);
 drawingPadLayout = (FrameLayout) findViewById(R.id.blacken_imageview);
 mDrawingView=new DrawingView(this);
 mDrawingView.setBackgroundDrawable(Drawable.createFromPath(imagePath)); // getting imagepath from previous activity.
 mDrawingView.setScaleType(ScaleType.FIT_CENTER);//I tried CENTER, CENTER_INSIDE i.e every scaleType. But it's doesn't change the ImageView Background scale type at all
 drawingPadLayout.addView(mDrawingView);
 }

无论scaleType应用于DrawingViewDrawingView背景图像正在拉伸到适合屏幕。看起来像DrawingView背景试图匹配它的父母,即 FrameLayout 我应该如何避免这些?。

我想图像背景不应该拉伸以匹配屏幕。它应该保持不变。

DashBoard Image Blacken Image Screen Applied Blacken with Touch Event


这是我的习惯ImageView,我过去常常触摸它。

class DrawingView extends ImageView {
Paint mPaint;
Bitmap mBitmap;
Canvas mCanvas;
Path mPath;
Paint mBitmapPaint;

public DrawingView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(20);

    mPath = new Path();
    mBitmapPaint = new Paint();
    mBitmapPaint.setColor(Color.BLACK);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
}

@Override
public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.draw(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);
    // mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
    // kill this so we don't double draw
    mPath.reset();
    // mPath= new Path();
}

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

1 个答案:

答案 0 :(得分:1)

根据Pskink,

setBackgroundDrawable替换为setImageDrawable解决了这个问题。 Image不再受到影响。它保持不变。

mDrawingView.setImageDrawable(Drawable.createFromPath(imagePath));