确定外部类中的屏幕大小然后调整手写捕获视图的笔触宽度以便它可以很好地缩放的最佳方法是什么?
我在外部库中有这个SignatureCanvas.java类。正如你所看到的,我已经将笔画宽度设置为一个常数,这是有效的(那里没什么奇怪的!)。然而,由于我一直在测试各种设备和电子邮件(屏幕大小不同),因此笔划宽度不会转换为任何类型的引擎盖下像素 - > dip(这是可以理解的!)。因此,在较旧/较小的屏幕尺寸上,此笔划宽度为8,实际上看起来像是14,并且签名变得不可读。
我已经集思广益了,想出了推广屏幕分辨率和每个'支架'的默认笔划宽度(可以这么说),然后在我需要的时候应用它。然而,这似乎有点垃圾,我想知道是否有人之前有过这种困境以及你是如何解决它的?
这个类纯粹是为了演示目的,代码本身没有任何问题。
package com.goosesys.gooselib.Views; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class SignatureCanvas extends View { private final float STROKE_WIDTH = 8f; private final boolean ANTI_ALIAS = true; private final int DEFAULT_PEN_COLOUR = 0xFF000000; private Path drawPath; private Paint drawPaint; private Paint canvasPaint; private int paintColour = DEFAULT_PEN_COLOUR; private Canvas drawCanvas; private Bitmap canvasBitmap; /* * Constructors */ // Main in-code constructor // public SignatureCanvas(Context context) { super(context); setupDrawing(); } // Constructor for use in UI layout tool - Custom Views public SignatureCanvas(Context context, AttributeSet attributeSet){ super(context, attributeSet); setupDrawing(); } /* * Methods */ private void setupDrawing(){ drawPath = new Path(); drawPaint = new Paint(); // set initial colour for drawing drawPaint.setColor(paintColour); // setup paths drawPaint.setAntiAlias(ANTI_ALIAS); drawPaint.setStrokeWidth(STROKE_WIDTH); drawPaint.setStyle(Paint.Style.STROKE); drawPaint.setStrokeJoin(Paint.Join.ROUND); drawPaint.setStrokeCap(Paint.Cap.ROUND); // finally create a new canvas paint object canvasPaint = new Paint(Paint.DITHER_FLAG); } public void clearCanvas(){ drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR); invalidate(); } public Bitmap saveSignature(){ return Bitmap.createBitmap(canvasBitmap); } /* * (non-Javadoc) * @see android.view.View#onSizeChanged(int, int, int, int) */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh){ super.onSizeChanged(w, h, oldw, oldh); // view given size canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); drawCanvas = new Canvas(canvasBitmap); } @Override protected void onDraw(Canvas canvas){ // draw view canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawPath(drawPath, drawPaint); } @Override public boolean onTouchEvent(MotionEvent event){ // detect user touch float touchX = event.getX(); float touchY = event.getY(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: drawPath.moveTo(touchX, touchY); break; case MotionEvent.ACTION_MOVE: drawPath.lineTo(touchX, touchY); break; case MotionEvent.ACTION_UP: drawCanvas.drawPath(drawPath, drawPaint); drawPath.reset(); break; default: return false; } invalidate(); return true; } }
答案 0 :(得分:1)
public static int convertDpsToPixels(Context context, int dps) {
// http://developer.android.com/guide/practices/screens_support.html
// Convert the dps to pixels
final float scale = context.getResources().getDisplayMetrics().density;
final float dpsFloat = dps;
return (int) (dpsFloat * scale + 0.5f);
}