0

我有两个问题。

1) 理想情况下,我希望为我的类的构造函数扩展此签名View,因为它适用于任何类型的布局。

public HexagonView(Context context, float radius, PointF origin, int background, Layout layout) {

但是,这行不通,因为我必须RelativeLayout像这样调用构造函数:

HexagonView hexagon = new HexagonView(this, 75, new PointF(200,300), Color.BLUE, (RelativeLayout) findViewById(R.id.activity_main));

可以将其更改为接受多个布局吗?例如, if(layout instanceof RelativeLayout) 然后进行转换。(这似乎不起作用)

2)我传递布局的原因是我以后可以轻松地将视图添加到同一个父布局。我有一种感觉,这不是一种安全的编程习惯。这可以吗,还是我应该以不同的方式解决这个问题?

编辑:我想我想出了我为什么不能通过它的第一个问题。我相信这是因为我假设LinearLayoutRelativeLayout扩展Layout了,但实际上它们扩展了ViewGroup

4

1 回答 1

0

要捕获签名,您必须采用线性或相对布局,您可以通过创建启用绘图缓存为 true 的方式在位图中捕获其签名,然后获取布局的绘图缓存并将其存储在位图中,然后将该位图显示在图像视图。

您可以从以下代码中获得帮助

首先创建一个扩展视图的内部类签名

public class signature extends View {
        private static final float STROKE_WIDTH = 5f;
        private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
        private Paint paint = new Paint();
        private Path path = new Path();
        private float lastTouchX;
        private float lastTouchY;
        private final RectF dirtyRect = new RectF();
        public signature(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
        }
        @Override
        protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
        }
        @Override
        public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        path.moveTo(eventX, eventY);
        lastTouchX = eventX;
        lastTouchY = eventY;
        return true;
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
        resetDirtyRect(eventX, eventY);
        int historySize = event.getHistorySize();
        for (int i = 0; i < historySize; i++) {
        float historicalX = event.getHistoricalX(i);
        float historicalY = event.getHistoricalY(i);
        expandDirtyRect(historicalX, historicalY);
        path.lineTo(historicalX, historicalY);
        }
        path.lineTo(eventX, eventY);
        break;
        default:
        debug("Ignored touch event: " + event.toString());
        return false;
        }
        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
        (int) (dirtyRect.top - HALF_STROKE_WIDTH),
        (int) (dirtyRect.right + HALF_STROKE_WIDTH),
        (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
        lastTouchX = eventX;
        lastTouchY = eventY;
        return true;
        }

        private void debug(String string) {
        }

        private void expandDirtyRect(float historicalX, float historicalY) {
        if (historicalX < dirtyRect.left) {
        dirtyRect.left = historicalX;
        } else if (historicalX > dirtyRect.right) {
        dirtyRect.right = historicalX;
        }
        if (historicalY < dirtyRect.top) {
        dirtyRect.top = historicalY;
        } else if (historicalY > dirtyRect.bottom) {
        dirtyRect.bottom = historicalY;
        }
        }
        private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
        }
    }

在主要活动中创建类签名的对象,例如

signature sign;
mLinearSign.addView(sign, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

当您想在程序中捕获签名时,请遵循以下代码行

 ImageView ImgVwSign;
    Bitmap sign = Bitmap.createBitmap(480, 800,Bitmap.Config.ARGB_8888);    
    mLinearSign.setDrawingCacheEnabled(true);
    sign =mContent.getDrawingCache();
    ImgVwSign.setImageBitmap(sign );
于 2013-10-22T04:28:28.960 回答