Android编程实现图片背景渐变切换与图层叠加效果

  

本文实例讲述了Android编程实现图片背景渐变切换与图层叠加效果。分享给大家供大家参考,具体如下:

  

本例要实现的目的:

  

1。图片背景渐变的切换,例如渐变的从红色切换成绿色。

  

2。代码中进行图层叠加,即把多个可拉的叠加在一起显示在一个组件之上。

  

效果图:

  

 Android编程实现图片背景渐变切换与图层叠加效果

  

代码很简单:

  

(1)布局文件:

        & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android: paddingLeft=" @dimen/activity_horizontal_margin”   android: paddingRight=" @dimen/activity_horizontal_margin”   android: paddingTop=" @dimen/activity_vertical_margin”   android: paddingBottom=" @dimen/activity_vertical_margin”   工具:忽视=" ContentDescription "   工具:上下文=?MainActivity”比;   & lt; ImageView   android: id=癅 + id/color_iv”   android: layout_width=" 200 dp”   android: layout_height=" 200 dp”   android: layout_centerHorizontal=" true "   android: src=" https://www.yisu.com/zixun/@drawable image_bg_2 "   android: layout_margin=" 20 dp/比;   & lt; TextView   android: id=癅 + id/note_text”   android: layout_below=癅 + id/color_iv”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: textSize=" 16 sp "   android: layout_margin=" 10 dp”   android:文本="点击颜色块,切换图片背景”/比;   LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=48“斜坡”,   android: layout_below=癅 + id/note_text”   android: layout_marginBottom=" 8下降”   android: layout_marginLeft=" 4下降”   android: layout_marginRight=" 4下降”   面向android:="水平"比;   & lt; ImageView   android: layout_width=" 0下降”   android: layout_height=" match_parent "   android: layout_margin=" 4下降”   android: layout_weight=" 1 "   android:背景=" # 99666666 "   android: onClick=" onColorClicked "   android:标签=" # 99666666 "/比;   & lt; ImageView   android: layout_width=" 0下降”   android: layout_height=" match_parent "   android: layout_margin=" 4下降”   android: layout_weight=" 1 "   android:背景=" # 9996 aa39 "   android: onClick=" onColorClicked "   android:标签=" # 9996 aa39 "/比;   & lt; ImageView   android: layout_width=" 0下降”   android: layout_height=" match_parent "   android: layout_margin=" 4下降”   android: layout_weight=" 1 "   android:背景=" # 99 c74b46 "   android: onClick=" onColorClicked "   android:标签=" # 99 c74b46 "/比;   & lt; ImageView   android: layout_width=" 0下降”   android: layout_height=" match_parent "   android: layout_margin=" 4下降”   android: layout_weight=" 1 "   android:背景=" # 99 f4842d "   android: onClick=" onColorClicked "   android:标签=" # 99 f4842d "/比;   & lt; ImageView   android: layout_width=" 0下降”   android: layout_height=" match_parent "   android: layout_margin=" 4下降”   android: layout_weight=" 1 "   android:背景=" # 993 f9fe0 "   android: onClick=" onColorClicked "   android:标签=" # 993 f9fe0 "/比;   & lt; ImageView   android: layout_width=" 0下降”   android: layout_height=" match_parent "   android: layout_margin=" 4下降”   android: layout_weight=" 1 "   android:背景=" # 995161 bc”   android: onClick=" onColorClicked "   android:标签=" # 995161 bc”/比;   & lt;/LinearLayout>   & lt;/RelativeLayout>      之前      

(2)活动代码:

        包com.sinatj.colorgradientanim;   进口android.graphics.Color;   进口android.graphics.drawable.ColorDrawable;   进口android.graphics.drawable.Drawable;   进口android.graphics.drawable.LayerDrawable;   进口android.graphics.drawable.TransitionDrawable;   进口android.os.Build;   进口handler;   进口android.support.v7.app.ActionBarActivity;   进口android.os.Bundle;   进口android.view.View;   进口android.widget.ImageView;   公开课MainActivity延伸ActionBarActivity {   私人ImageView ImageView;   私人可拉的oldBackground=零;   私人可拉的bgDrawable;   @Override   保护无效onCreate(包savedInstanceState) {   super.onCreate (savedInstanceState);   setContentView (R.layout.activity_main);   imageView=(imageView) findViewById (R.id.color_iv);   bgDrawable=getresource () .getDrawable (R.drawable.image_bg_1);//初始颜色   changeColor (Color.parseColor (" # 6696 aa39 "));   }   私人空间changeColor (int newColor) {   可拉的colorDrawable=new colorDrawable (newColor);//图层叠加   LayerDrawable ld=new LayerDrawable(新可拉的[]{bgDrawable, colorDrawable});   如果(oldBackground==null) {   imageView.setBackgroundDrawable (ld);   其他}{//渐变切换   TransitionDrawable td=new TransitionDrawable(新可拉的[]{oldBackground, ld});   imageView.setBackgroundDrawable (td);   td.startTransition (300);   }   oldBackground=ld;   }   公共空间onColorClicked(查看v) {   .toString int颜色=Color.parseColor (v.getTag () ());   changeColor(颜色);   }   }      

Android编程实现图片背景渐变切换与图层叠加效果