首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android中图像的动态热点设计

Android中图像的动态热点设计
EN

Stack Overflow用户
提问于 2012-10-11 20:08:17
回答 1查看 1.4K关注 0票数 3

我必须像下面这样开发一个UI:

我想要显示这种类型的图像,并在该图像上显示热点。热点的位置将是动态的,根据x,y和半径,假设圆将绘制在原始图片上。用户可以点击热点,并且将在用户将点击的特定热点上定义onclick动作。

开发这种类型的UI的最佳流程是什么?

EN

回答 1

Stack Overflow用户

发布于 2012-10-17 07:21:18

将主布局设置为RelativeLayout,然后可以使用以下代码以编程方式将具有onClickListenerImageView添加到您的布局中:

代码语言:javascript
复制
private void addImageView(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){
    ImageView imageView = new ImageView(this);
    imageView.setAdjustViewBounds(false);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.height = height;
    params.width = width;
    imageView.setLayoutParams(params);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);  //remove this if you want to keep aspect ratio
    imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); //here goes your drawable
    params.leftMargin = x - width/2;
    params.topMargin = y - height/2;
    imageView.setOnClickListener(onClickListener);
    mainLayout.addView(imageView);
}

要使用它,您可以调用:

代码语言:javascript
复制
    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //this is your main layout
    addImageButton(mainLayout, 200, 300, 200, 200, new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
        }
    });

您也可以使用ImageButton来实现相同的姿势,尽管图像大小会受到按钮边框的影响:

代码语言:javascript
复制
private void addImageButton(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){
    ImageButton imageButton = new ImageButton(this);
    imageButton.setAdjustViewBounds(true);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.height = height;
    params.width = width;
    imageButton.setLayoutParams(params);
    imageButton.setScaleType(ImageView.ScaleType.FIT_XY);
    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
    params.leftMargin = x - width/2;
    params.topMargin = y - height/2;
    imageButton.setOnClickListener(onClickListener);
    mainLayout.addView(imageButton);
}

试试看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12839182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档