Android 设置图片颜色的透明度

     Android系统支持的颜色是由4个值组成的,前3个为RGB,也就是我们常说的三原色(红、绿、蓝),最后一个值是A,也就是Alpha。这4个值都在0~255之间。颜色值越小,表示该颜色越淡,颜色值越大,表示该颜色越深。如果RGB都是0,就是黑色,如果都为255,就是白色。Alpha也需要在0~255之间变化。Alpha的值越小,颜色就越透明,Alpha的值越大,颜色就不透明。当Alpha的值为0时,颜色完全透明,完全透明的位图或者图形从View上消失。当Alpha的值为255时,颜色不透明。从Alpha的特性可知,设置颜色的透明度实际上就是设置Alpha值。

 

1.我们可以根据后台返回的数据来相应的设置图片和文字对应的状态,比如:当一个num数值大于0时,图片显示为正常的颜色,也就是:

布局中设置:

<ImageView
                android:id="@+id/gemstone"
                android:layout_width="wrap_content"
                android:alpha="255"
                android:layout_height="wrap_content"
                android:src="@drawable/gemstone" />

 或者代码中设置:

  gemstone.setAlpha(255);

2.当num小于等于0时则设置:图片显示成带有透明度的样子:

布局中设置:

<ImageView
                android:id="@+id/gemstone"
                android:layout_width="wrap_content"
                android:alpha="0.5"
                android:layout_height="wrap_content"
                android:src="@drawable/gemstone" />

 或者代码中设置:

 gemstone.setAlpha(90);

 

 <LinearLayout
            android:id="@+id/starLl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/bottomCl"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="34dp"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/gemstone"
                android:layout_width="wrap_content"
                android:alpha="0.5"
                android:layout_height="wrap_content"
                android:src="@drawable/gemstone" />

            <TextView
                android:id="@+id/lastHarvestAmount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:text="@string/mining"
                android:textColor="#333"
                android:textSize="9dp" />
        </LinearLayout>

再举个例子:SeekBar来改变图中颜色的Alpha值(透明度),显示位图的MyView类的代码如下:

private class MyView extends View

    {

       private Bitmap bitmap;

       public MyView(Context context)

       {

           super(context);

           InputStream is =getResources().openRawResource(R.drawable.image);

           bitmap = BitmapFactory.decodeStream(is);

           setBackgroundColor(Color.WHITE);

       }

       @Override

       protected void onDraw(Canvas canvas)

       {

           Paint paint = new Paint();

           paint.setAlpha(alpha);

           canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap

                  .getHeight()), new Rect(10, 10, 310, 235), paint);

       }

    }

SeekBar:

public void onProgressChanged(SeekBar seekBar, int progress,

           boolean fromUser)
    {

       alpha = progress;

       setTitle("alpha:" + progress);

       myView.invalidate();

    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值