将不同类型的布局传递给View

时间:2013-10-22 03:36:38

标签: java android layout

我有两个问题。

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

可以更改为接受多个布局吗?例如,如果(布局instanceof RelativeLayout)然后投射它。 (这似乎不起作用)

2)我传递布局的原因是我以后可以轻松地将视图添加到相同的父布局。我觉得这不是一个安全的编程实践。这样可以,或者我应该采用不同的方式进行此操作?

编辑:我想我发现了我的第一个问题,为什么我无法通过它。我认为这是因为我假设LinearLayoutRelativeLayout延长Layout,但实际上他们延长了ViewGroup

1 个答案:

答案 0 :(得分:0)

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

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

首先创建一个扩展View

的内部类签名
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 );