简体   繁体   English

有没有一种方法可以在运行时创建的Android中制作异形按钮?

[英]Is there a way to make shaped buttons in Android that you create in run time?

We are creating an app where you can create shaped buttons at run time to map images, something like this: http://www.outsharked.com/imagemapster/default.aspx?demos.html#vegetables We try with Android Paths but you can not make the paths disappear or change the color. 我们正在创建一个应用程序,您可以在运行时创建带有形状的按钮来映射图像,例如: http ://www.outsharked.com/imagemapster/default.aspx?demos.html#vegetables我们尝试使用Android Paths,但您可以不能使路径消失或改变颜色。

Do you have an idea? 你有想法吗?

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

I haven't practiced yet but I have found a solution. 我还没有练习过,但是找到了解决方案。 Since android studio supports javascript and css, you can do what you asked. 由于android studio支持javascript和CSS,因此您可以执行自己要求的操作。 In your given link, you can find the javascript and css file of your intended task. 在给定的链接中,您可以找到所需任务的javascript和css文件。 Then you can follow this given link where it gives all the information about adding javascript and css in Android Studio. 然后,您可以点击此给定的链接,其中提供了有关在Android Studio中添加javascript和css的所有信息。 Follow this link . 点击此链接

You can use a touch delegate (it's in the Android SDK) to achieve this. 您可以使用触摸委托(在Android SDK中)来实现。 You'll probably have a hard time creating your shape, but you can give it a go :) 您可能很难创建形状,但是可以尝试一下:)

Here's an excerpt from Vogella's tutorial ( http://blog.vogella.com/2012/04/15/android-using-touchdelegates/ ): 这是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);
                }
            };
        });
    }

Good luck! 祝好运!

The only way that do this is making a call of invalidate() in this way the onDraw () is called and redraw the entire view using the changes that you wanted. 唯一的方法就是以这种方式调用invalidate(),然后调用onDraw()并使用所需的更改重新绘制整个视图。

The canvas is not valid after the onDraw() is finished. onDraw()完成后,画布无效。

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM