我一直在创建图像处理应用程序。图像变黑的地方(技术上使用paint
和path
在TouchEvent
上在画布上绘制)。
这是我用于涂黑屏幕的布局
<?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>
在变黑活动中:
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
应用于DrawingView
。 DrawingView
背景的图像被拉伸到“适合屏幕”。好像DrawingView背景试图匹配它的父级,即FrameLayout我应该如何避免这些?我希望图像背景不应该拉伸以匹配屏幕。它应该保持原样。
这是我自定义的
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;
}
}
最佳答案
根据Pskink,
将setBackgroundDrawable
替换为setImageDrawable
解决了该问题。 Image
不再被拉伸。它仍然完好无损。
mDrawingView.setImageDrawable(Drawable.createFromPath(imagePath));