有没有办法在Android中创建您在运行时创建的形状按钮?

时间:2016-02-25 04:23:02

标签: java android

我们正在创建一个应用程序,您可以在运行时创建形状按钮来映射图像,如下所示: http://www.outsharked.com/imagemapster/default.aspx?demos.html#vegetables 我们尝试使用Android路径但您无法使路径消失或更改颜色。

你知道吗?

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Botones btn = new Botones(this);

        setContentView(btn);

    }

    private class Botones extends View {

        ArrayList<Path> paths = new ArrayList<Path>();
        ArrayList<Paint> paints = new ArrayList<Paint>();

        public Botones(Context context) {
            super(context);
        }
        public Paint paintn;
        public Paint paintb;
        public Canvas canvasg;
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvasg = canvas;
            paintn = new Paint();
            paintn.setColor(getColor(R.color.colorAccent));
            paintn.setStyle(Paint.Style.STROKE);
            paintn.setStrokeWidth(8);
            paintn.setStyle(Paint.Style.FILL);
            paints.add(paintn);

            paintb = new Paint();
            paintb.setColor(getColor(R.color.linea));
            paintb.setStyle(Paint.Style.STROKE);
            paintb.setStrokeWidth(8);
            paintb.setStyle(Paint.Style.FILL);
            paints.add(paintb);



            coordenadas(canvas);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {

            float x;
            float y;
            x = event.getX();
            y = event.getY();

            for (Path p : paths){
                RectF pBounds=new RectF();
                p.computeBounds(pBounds,true);
                if (pBounds.contains(x,y)){
                    Log.i("path", "tocado");
                    //paints.get(0).setColor(getColor(R.color.linea));
                    canvasg.drawPath(p, paintb);
                }
            }

            return super.onTouchEvent(event);
        }

        public void coordenadas(Canvas canvas) {

            int i;
            boolean tipo = true;

            String cadena = "13, 421, 7, 587, 274, 590, 262, 467, 106, 506, 65, 444, 12, 416" +
                "|81, 13, 95, 55, 378, 70, 368, 4" +
                "|543, 88, 522, 218, 751, 228, 750, 133, 602, 111" +
                "|34, 268, 33, 383, 278, 389, 433, 356, 429, 288, 281, 312" +
                "|46, 55, 21, 131, 119, 172, 275, 158, 273, 106, 89, 79";
            String cadenaArray[] = cadena.split("\\|");

            for (String img : cadenaArray) {

                ArrayList<Float> x = new ArrayList<Float>();
                ArrayList<Float> y = new ArrayList<Float>();

                String imgArray[] = img.split("\\,");
                for (String coordena : imgArray){
                    float fin = Float.parseFloat(coordena);
                    if (tipo){
                        x.add(fin);
                    } else {
                        y.add(fin);
                    }
                    tipo = !tipo;

                }

                dibujar(x,y, canvas);
            }
        }

        public void dibujar(ArrayList<Float> coordx, ArrayList<Float> coordy, Canvas canvas){
            Path path =new Path();
            paths.add(path);
            canvas.drawColor(getColor(android.R.color.transparent));

            path.moveTo(coordx.get(0), coordy.get(0));

            for (int z = 1; z < coordx.size(); z++){
                path.lineTo(coordx.get(z),coordy.get(z));
            }
            path.lineTo(coordx.get(0),coordy.get(0));
            canvas.drawPath(path, paintn);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

我还没有练习,但我找到了解决方案。由于android studio支持javascript和css,你可以做你所要求的。在给定的链接中,您可以找到预期任务的javascript和css文件。然后,您可以按照这个给定的链接,它提供有关在Android Studio中添加javascript和css的所有信息。请遵循此link

答案 1 :(得分:0)

您可以使用触摸代理(Android SDK中的代理)来实现此目的。你可能很难创造你的形状,但你可以试一试:)

这里是Vogella教程(http://blog.vogella.com/2012/04/15/android-using-touchdelegates/)的摘录:

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton = (Button) findViewById(R.id.delegated_button);
        View parent = findViewById(R.id.root);
        parent.post(new Runnable() {
            public void run() {
                // Post in the parent's message queue to make sure the parent
                // lays out its children before we call getHitRect()
                Rect delegateArea = new Rect();
                Button delegate = mButton;
                delegate.getHitRect(delegateArea);
                delegateArea.top -= 600;
                delegateArea.bottom += 600;
                delegateArea.left -= 600;
                delegateArea.right += 600;
                TouchDelegate expandedArea = new TouchDelegate(delegateArea,
                        delegate);
                // give the delegate to an ancestor of the view we're
                // delegating the
                // area to
                if (View.class.isInstance(delegate.getParent())) {
                    ((View) delegate.getParent())
                            .setTouchDelegate(expandedArea);
                }
            };
        });
    }
祝你好运!

答案 2 :(得分:0)

执行此操作的唯一方法是以这种方式调用invalidate(),调用onDraw()并使用所需的更改重绘整个视图。

onDraw()完成后,画布无效。

我希望这会有所帮助