开发者

Set OnClickListener to View drawing a Bitmap painted by Canvas

I do not get a grip on solving the issue.

I have manually placed two images on the screen. I would like to have an onClick event handling for each of it. By the following approach, the OnClick Handler seems to be valid only for the background. Acually, all created sub-views/Imagers and their onClickListeners are reacting on the parent where they have drawn onto. The AndroidManifext.xml is only the RelativeLayout with no child tags.

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Imager theUpperImage = new Imager( R.drawable.img1, -25, -100, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeTe开发者_Python百科xt( v.getContext(), "Upper", Toast.LENGTH_SHORT).show();
            }
        });

        addContentView( theUpperImage, new View.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        final Imager theLowerImage = new Imager( R.drawable.img2, -25, +50, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText( v.getContext(), "Lower", Toast.LENGTH_SHORT).show();
            }
        });

        addContentView( theLowerImage, new View.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

    class Imager extends View {

        final int resImage;
        final int centerOffsetX, centerOffsetY;

        public Imager(int inResImage, int inCenterOffsetX, int inCenterOffsetY, View.OnClickListener inOnClickListener) {
            super( Main.this );
            resImage = inResImage;
            centerOffsetX = inCenterOffsetX;
            centerOffsetY = inCenterOffsetY;
            super.setOnClickListener( inOnClickListener );
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resImage);
            final int x = (canvas.getWidth()/2)+centerOffsetX;
            final int y = (canvas.getWidth()/2)+centerOffsetY;
            canvas.drawBitmap(bitmap, x, y, null);

        }

        @Override
        public void draw(Canvas canvas) {
            super.draw( canvas );
            final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resImage);
            final int x = (canvas.getWidth()/2)+centerOffsetX;
            final int y = (canvas.getWidth()/2)+centerOffsetY;
            canvas.drawBitmap(bitmap, x, y, null);

        }
    }   
}

How to created freely placed images and provide them with an OnClick-functionality?

I would not stick to the above solution. Important for me is to freely place the images on the screen and have them clickable. The predefined Layouts semm not to give me a certain Layout which have a FreeLayout-functionality. (Or is the intention of Layout misleading me?)

I also keep in mind that I probably want to dynamicly remove an Image later, but I am not there yet.

Thank you a lot for help in advance, Dirk

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜