如何做到imageViewps局部透明明

17184人阅读
Android(97)
经常用到多个透明图片层叠,但又需要获取不同图片的点击事件,本文实现图片透明区域穿透点击事件。
&欢迎转载请说明转自:
一、先上图片,这2张图片尺寸是一样的,放到drawable目录下:
图1:transparent.png
图2:transparent2.png
二、上布局文件,test.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&FrameLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical&
android:gravity=&center&
android:id=&@+id/mainLayout&&
&ImageView android:id=&@+id/ImageView01&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:src=&@drawable/transparent&/&
&ImageView android:id=&@+id/ImageView02&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:src=&@drawable/transparent2&/&
&/FrameLayout&
三、上Activity文件,MainActivity.java
package org.shuxiang.
import android.app.A
import android.graphics.B
import android.graphics.drawable.BitmapD
import android.os.B
import android.util.L
import android.view.MotionE
import android.view.V
import android.view.W
import android.view.View.OnClickL
import android.view.View.OnTouchL
import android.widget.ImageV
public class MainActivity extends Activity
private ImageView iv1;
private ImageView iv2;
private Bitmap bitmap1, bitmap2;
private boolean iv1Transparent =
private boolean iv2Transparent =
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
iv1 = (ImageView) findViewById(R.id.ImageView01);
iv2 = (ImageView) findViewById(R.id.ImageView02);
bitmap1 = ((BitmapDrawable) (iv1.getDrawable())).getBitmap();
bitmap2 = ((BitmapDrawable) (iv2.getDrawable())).getBitmap();
iv1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
if(iv1Transparent)
Log.i(&test&, &图1透明区域&);
Log.i(&test&, &图1点击&);
iv1.setOnTouchListener(new OnTouchListener()
public boolean onTouch(View arg0, MotionEvent arg1)
// TODO Auto-generated method stub
if(bitmap1.getPixel((int)(arg1.getX()),((int)arg1.getY()))==0)
Log.i(&test&, &图1透明区域&);
iv1Transparent = //透明区域设置true
Log.i(&test&, &图1实体区域&);
iv1Transparent =
iv2.setOnClickListener(new OnClickListener()
public void onClick(View v) {
// TODO Auto-generated method stub
if(iv2Transparent)
Log.i(&test&, &图2透明区域&);
Log.i(&test&, &图2点击&);
iv2.setOnTouchListener(new OnTouchListener()
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(bitmap2.getPixel((int)(event.getX()),((int)event.getY()))==0)
Log.i(&test&, &图2透明区域&);
iv2Transparent = //透明区域设置true
iv1.dispatchTouchEvent(event);
Log.i(&test&, &图2实体区域&);
iv2Transparent =
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2822470次
积分:13225
积分:13225
排名:第661名
原创:97篇
转载:92篇
评论:229条
(1)(1)(1)(1)(1)(1)(1)(4)(1)(1)(1)(2)(1)(1)(3)(1)(10)(8)(2)(1)(1)(1)(1)(1)(2)(3)(2)(68)(30)(24)(37)(2)12855人阅读
Android(43)
在项目中,需要把RelativeLayout 和 &ImageView背景设置为透明,怎么实现呢?这里主要通过代码,请参阅以下关键代码:
public ImageView imgD
private RelativeLayout rlM
rlMain= (RelativeLayout) findViewById(R.id.rlMain);//布局
imgDetail = (ImageView)findViewById(R.id.imgDetail); // 图片
//设置背景
rlMain.setBackgroundColor(Color.TRANSPARENT);
imgDetail.setBackgroundColor(Color.TRANSPARENT);
主要用到类 :&
android.graphics.Color 和 android.view.View 类的setBackgroundColor方法!
android中其他继承android.view.View 类,背景设置为透明的方法也类似!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1220779次
积分:12533
积分:12533
排名:第736名
原创:248篇
转载:12篇
评论:124条
(2)(5)(2)(6)(1)(1)(3)(7)(3)(5)(5)(3)(2)(12)(4)(6)(18)(17)(14)(21)(14)(2)(3)(8)(2)(8)(12)(5)(28)(32)(7)(1)(1)Android使用ImageView 制作透明圆弧实例代码
作者:monkey1992
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Android使用ImageView 制作透明圆弧实例代码的相关资料,需要的朋友可以参考下
这几天因为项目需求,需要在ImageView上面叠加一层透明圆弧,并且在沿着圆弧的方向显示相应的文字,效果如下图所示:
  拿到这个需求,首先想到的是自定义一个ImageView来实现此功能,即在onDraw()中绘制圆弧和文字。同时因为要保证圆弧的位置可以任意摆放,圆弧的颜色、透明度以及文字大小、颜色等都是可控的,所以增加了一些自定义属性。实现代码非常简单,如下:
1.自定义ImageView:
package com.chunk.customviewsdemo.views.ArcImageV
import android.content.C
import android.content.res.TypedA
import android.graphics.C
import android.graphics.P
import android.graphics.P
import android.graphics.RectF;
import android.util.AttributeS
import android.widget.ImageV
import com.chunk.customviewsdemo.R;
* Description:A custom ImageView with circular arc and text
* Author: XiaoYu
* Date: // :
public class ArcImageView extends ImageView {
* The default text size.
private final float DEFAULT_TEXT_SIZE = ;
* The default scale value which decides the width of arc.
private final float DEFAULT_SCALE = .f;
* The default transparency of arc.
private final int DEFAULT_ARC_ALPHA =;
* The default width of arc.
private final int DEFAULT_ARC_WIDTH =;
* The default angle that the arc starts with.
private final int DEFAULT_START_ANGLE = ;
* The default angle that the arc.
private final int DEFAULT_SWEEP_ANGLE = ;
* The default distance along the path to add to the text's starting position.
private final int DEFAULT_H_OFFSET = ;
* The default distance above(-) or below(+) the path to position the text.
private final int DEFAULT_V_OFFSET = ;
private Context mC
* The text displayed on ImageView along arc.
private String mDrawS
* The font size of text.
private float mTextSize = DEFAULT_TEXT_SIZE;
* The scale value which decides the width of arc.
private float mScale = DEFAULT_SCALE;
* The transparency of arc.
private int mArcAlpha = DEFAULT_ARC_ALPHA;
* The width of arc.
private int mArcWidth = DEFAULT_ARC_WIDTH;
* The start angle of angle.
private int mStartAngle = DEFAULT_START_ANGLE;
* The swept angle of angle.
private int mSweepAngle = DEFAULT_SWEEP_ANGLE;
* The default distance along the path to add to the text's starting position.
private float mHOffset = DEFAULT_H_OFFSET;
* The default distance above(-) or below(+) the path to position the text.
private float mVOffset = DEFAULT_V_OFFSET;
* The style of arc, all styles includes LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM, CENTER。
* of course, you can add your own style according to your demands.
private int mDrawS
* The color of arc.
private int mArcC
* The color of text.
private int mTextC
public ArcImageView(Context context) {
super(context);
this.mContext =
public ArcImageView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext =
obtainAttributes(attrs);
public ArcImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext =
obtainAttributes(attrs);
* Set the text that will be drawn on arc.
* @param drawStr the text content.
public void setDrawStr(String drawStr) {
this.mDrawStr = drawS
//refresh this view
invalidate();
* Set the transparency of arc.
* @param mArcAlpha the value of transparency.
public void setArcAlpha(int mArcAlpha) {
this.mArcAlpha = mArcA
//refresh this view
invalidate();
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//draw arc
Paint arcPaint = new Paint();
arcPaint.setStrokeWidth(mArcWidth);
arcPaint.setStyle(Paint.Style.STROKE);
arcPaint.setColor(mArcColor);
arcPaint.setAlpha(mArcAlpha);
int width = getWidth();
int height = getHeight();
if (width & height) {
radius = mScale *
radius = mScale *
RectF oval = new RectF();
int center_x =
int center_y =
switch (mDrawStyle) {
center_x = ;
center_y = ;
mStartAngle = ;
mSweepAngle = -;
center_x = ;
center_y =
mStartAngle = ;
mSweepAngle = ;
center_x =
center_y = ;
mStartAngle = ;
mSweepAngle = -;
center_x =
center_y =
mStartAngle = ;
mSweepAngle = ;
center_x = width / ;
center_y = height / ;
mStartAngle = ;
mSweepAngle = ;
float left = center_x -
float top = center_y -
float right = center_x +
float bottom = center_y +
oval.set(left, top, right, bottom);
canvas.drawArc(oval, mStartAngle, mSweepAngle, false, arcPaint);
//draw text
Paint textPaint = new Paint();
textPaint.setTextSize(mTextSize);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(mTextColor);
Path path = new Path();
path.addArc(oval, mStartAngle, mSweepAngle);
canvas.drawTextOnPath(mDrawStr, path, mHOffset, mVOffset, textPaint);
* Obtain custom attributes that been defined in attrs.xml.
* @param attrs A collection of attributes.
private void obtainAttributes(AttributeSet attrs) {
TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.ArcImageView);
mDrawStr = ta.getString(R.styleable.ArcImageView_drawStr);
mTextSize = ta.getDimension(R.styleable.ArcImageView_textSize, DEFAULT_TEXT_SIZE);
mArcAlpha = ta.getInteger(R.styleable.ArcImageView_arcAlpha, DEFAULT_ARC_ALPHA);
mArcWidth = ta.getInteger(R.styleable.ArcImageView_arcWidth, DEFAULT_ARC_WIDTH);
mStartAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_START_ANGLE);
mSweepAngle = ta.getInteger(R.styleable.ArcImageView_startAngle, DEFAULT_SWEEP_ANGLE);
mHOffset = ta.getInteger(R.styleable.ArcImageView_hOffset, DEFAULT_H_OFFSET);
mVOffset = ta.getInteger(R.styleable.ArcImageView_vOffset, DEFAULT_V_OFFSET);
mArcColor = ta.getColor(R.styleable.ArcImageView_arcColor, XCCCCCC);
mTextColor = ta.getColor(R.styleable.ArcImageView_textColor, XFFFFFF);
mDrawStyle = ta.getInt(R.styleable.ArcImageView_drawStyle, );
ta.recycle();
2.在values文件夹下的attrs.xml中自定义属性:
&?xml version="." encoding="utf-"?&
&resources&
&declare-styleable name="ArcImageView"&
&attr name="drawStr" format="string" /&
&attr name="textSize" format="dimension" /&
&attr name="arcAlpha" format="integer" /&
&attr name="arcWidth" format="integer" /&
&attr name="startAngle" format="integer" /&
&attr name="sweepAngle" format="integer" /&
&attr name="scale" format="float" /&
&attr name="hOffset" format="float" /&
&attr name="vOffset" format="float" /&
&attr name="drawStyle" format="enum"&
&enum name="LEFT_TOP" value="" /&
&enum name="LEFT_BOTTOM" value="" /&
&enum name="RIGHT_TOP" value="" /&
&enum name="RIGHT_BOTTOM" value="" /&
&enum name="CENTER" value="" /&
&attr name="arcColor" format="color" /&
&attr name="textColor" format="color" /&
&/declare-styleable&
&/resources&
3.在MainActivity调用ArcImageView,实现代码如下:
package com.chunk.
import android.os.B
import android.support.v.app.AppCompatA
import android.view.V
import android.widget.B
import com.chunk.customviewsdemo.views.ArcImageView.ArcImageV
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ArcImageView aiv_
private ArcImageView aiv_
private ArcImageView aiv_
private ArcImageView aiv_
private Button btn_another_
private int mGroup = ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aiv_one = (ArcImageView) findViewById(R.id.aiv_one);
aiv_one.setArcAlpha();
aiv_two = (ArcImageView) findViewById(R.id.aiv_two);
aiv_two.setArcAlpha();
aiv_three = (ArcImageView) findViewById(R.id.aiv_three);
aiv_three.setArcAlpha();
aiv_four = (ArcImageView) findViewById(R.id.aiv_four);
aiv_four.setArcAlpha();
btn_another_one = (Button) findViewById(R.id.btn_another_one);
btn_another_one.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_another_one:
if (mGroup == ) {
aiv_one.setDrawStr("苹果");
aiv_one.setBackgroundResource(R.drawable.apple);
aiv_two.setDrawStr("柚子");
aiv_two.setBackgroundResource(R.drawable.pineapple);
aiv_three.setDrawStr("香蕉");
aiv_three.setBackgroundResource(R.drawable.banana);
aiv_four.setDrawStr("菠萝");
aiv_four.setBackgroundResource(R.drawable.pineapple);
mGroup = ;
aiv_one.setDrawStr("牛排");
aiv_one.setBackgroundResource(R.drawable.steak);
aiv_two.setDrawStr("海鲜");
aiv_two.setBackgroundResource(R.drawable.seafood);
aiv_three.setDrawStr("奶酪");
aiv_three.setBackgroundResource(R.drawable.cheese);
aiv_four.setDrawStr("烧烤");
aiv_four.setBackgroundResource(R.drawable.barbecue);
mGroup = ;
4.MainActivity的布局文件如下:
&LinearLayout
xmlns:android="/apk/res/android"
xmlns:custom="/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="dp"
android:layout_marginBottom="dp"
android:orientation="vertical" &
android:id="@+id/btn_another_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="换一组" /&
&LinearLayout
android:layout_width="match_parent"
android:layout_height="dp"
android:layout_weight=""
android:orientation="horizontal" &
&RelativeLayout
android:layout_width="dp"
android:layout_weight=""
android:layout_height="match_parent" &
&com.chunk.customviewsdemo.views.ArcImageView.ArcImageView
android:id="@+id/aiv_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/steak"
custom:drawStyle="RIGHT_BOTTOM"
custom:drawStr="牛排"
custom:arcAlpha=""
custom:arcColor="@color/gray"
custom:textColor="@color/black"
custom:textSize="sp" /&
&/RelativeLayout&
&RelativeLayout
android:layout_width="dp"
android:layout_weight=""
android:layout_height="match_parent" &
&com.chunk.customviewsdemo.views.ArcImageView.ArcImageView
android:id="@+id/aiv_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/seafood"
custom:drawStyle="LEFT_BOTTOM"
custom:drawStr="海鲜"
custom:arcAlpha=""
custom:arcColor="@color/gray"
custom:textColor="@color/black"
custom:textSize="sp" /&
&/RelativeLayout&
&/LinearLayout&
&LinearLayout
android:layout_width="match_parent"
android:layout_height="dp"
android:layout_weight=""
android:orientation="horizontal" &
&RelativeLayout
android:layout_width="dp"
android:layout_weight=""
android:layout_height="match_parent" &
&com.chunk.customviewsdemo.views.ArcImageView.ArcImageView
android:id="@+id/aiv_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cheese"
custom:drawStyle="RIGHT_TOP"
custom:drawStr="奶酪"
custom:arcAlpha=""
custom:arcColor="@color/gray"
custom:textColor="@color/black"
custom:textSize="sp" /&
&/RelativeLayout&
&RelativeLayout
android:layout_width="dp"
android:layout_weight=""
android:layout_height="match_parent" &
&com.chunk.customviewsdemo.views.ArcImageView.ArcImageView
android:id="@+id/aiv_four"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/barbecue"
custom:drawStyle="LEFT_TOP"
custom:drawStr="烧烤"
custom:arcAlpha=""
custom:arcColor="@color/gray"
custom:textColor="@color/black"
custom:textSize="sp" /&
&/RelativeLayout&
&/LinearLayout&
&/LinearLayout&
注意,在布局文件中引入自定义属性时需要加入一行代码:xmlns:custom="/apk/res-auto"。
好了,需求搞定,剩下的就是搬到实际的项目当中去了。实现效果如下:
总结一下,自定义View一般就是通过重写onDraw、onMeasure()、onLayout()等方法来进行测量、绘制,绘制的时候一般会用到Canvas、Paint、Bitmap等类,测量和绘制的过程其实就是对现实生活中绘图工作的抽象和实现,我们利用面向对象的思想将画板、画纸、画笔等工具以及绘画的动作用一行行代码加以描述就OK啦!
由于实现过程比较简单,我就不贴源码了,大家如果对2D绘图还不是很了解,可以去搜一下相关资料或查阅相关书籍!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具iOS半透明新手引导 手把手教你做
一、效果展示
这种类型的新手引导比较常见,用于告诉用户某个按钮的作用,或者提醒用户可以进行某种交互操作。引导样式是在界面上加了一个半透明的引导图,高亮部分就是要突出的区域
二、怎么做?
方案有很多种,不同的方案有不同的优缺点,这里列举两种常见的方案
1. 方案一:生成整张引导图
(1). 导出引导图
让设计师导出各个尺寸的引导图,引导图只包含指引部分,不包括背景,导出的引导图样式如下:
需要导出iPhone4,iPhone5,iPhone6,iPhone6 plus 共4个尺寸,如果适配iPad,还需要导出iPad的
因为整张图导出了,所以代码部分就简单了,不需要考虑布局问题,直接生成一个imageView放上去,然后给它添加个点击事件即可
代码如下:
-&(void)addGuideView&{&&&&&NSString&*imageName&=&&&&&&if&(DEVICE_IS_IPHONE5)&{&&&&&&&&&imageName&=&@&guide-568h&;&&&&&}&else&{&&&&&&&&&imageName&=&@&guide&;&&&&&}&&&&&&UIImage&*image&=&[UIImage&imageNamed:imageName];&&&&&UIImageView&*imageView&=&[[UIImageView&alloc]&initWithImage:image];&&&&&imageView.frame&=&self.view.&&&&&imageView.userInteractionEnabled&=&YES;&&&&&&UITapGestureRecognizer&*tap&=&[[UITapGestureRecognizer&alloc]&initWithTarget:self&action:@selector(dismissGuideView)];&&&&&[imageView&addGestureRecognizer:tap];&&&&&&[self.view&addSubview:imageView];&}&
这里需要特别注意:
@3x 的图片需要iOS8以上的系统才能够自动识别出来,如果要向前兼容,则图片名那里需要自行判断设备类型,然后指定对应的图片名称
(3). 优缺点
这种方案的优点是
只要设计师做好效果图以后,把蒙层导出成各种规格即可,90%的工作量都在设计师身上,程序员只需要简单地添加视图和事件即可
b. 维护成本低
当界面发生变化,或者引导图需要调整时,只需要设计师重新生成图片,然后替换就可以了
但这种方案的缺点也很多:
a. 图片占据空间大
每种设备一张图片,图片还是全屏规格的,可能还要适配横屏,明显会增加app安装包的大小
b. 图片无法复用
一张引导图只能用于一个地方,其他地方不可能会用得上
2. 方案二:图片拼接
图片拼接的思路是这样的,通过若干张图片拼成一个遮罩层,然后再在上面放其他元素,如下图所示
(1). 准备图片
这里需要准备3张图片
a. 空心的椭圆遮罩层
中间是透明,周围是白色的,白色部分可以在遮罩过程中修改成任意的颜色
c. 确定按钮
顶一下(0) 踩一下(0)
热门标签:如何为Android的ImageView和GrilView设置透明背景色
[问题点数:40分]
如何为Android的ImageView和GrilView设置透明背景色
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2010年8月 移动平台大版内专家分月排行榜第二2010年4月 移动平台大版内专家分月排行榜第二
2010年9月 移动平台大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 html image 局部放大 的文章

 

随机推荐