Android: Picture background gradient toggle and layer overlay implementation

Source: Internet
Author: User

The purpose of this example is to achieve:

1. Change the background gradient of the picture, for example, change the gradient from red to green.

2. Layer overlay in the code, which overlays multiple drawable on top of one component.



The code is simple:

(1) Layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" Android: paddingtop= "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_margin" tools: Ignore= "ContentDescription" tools:context= ". Mainactivity "> <imageview android:id=" @+id/color_iv "android:layout_width=" 200DP "Android:la yout_height= "200DP" android:layout_centerhorizontal= "true" android:src= "@drawable/image_bg_2" Android         : layout_margin= "20DP"/> <textview android:id= "@+id/note_text" android:layout_below= "@+id/color_iv"        Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "16SP" Android:layOut_margin= "10DP" android:text= "click on the color block, switch the picture background"/> <linearlayout android:layout_width= "Match_parent" android:layout_height= "48dip" android:layout_below= "@+id/note_text" android:layout_marginbottom= "8dip "Android:layout_marginleft=" 4dip "android:layout_marginright=" 4dip "android:orientation=" Horizontal "            > <imageview android:layout_width= "0dip" android:layout_height= "Match_parent" Android:layout_margin= "4dip" android:layout_weight= "1" android:background= "#99666666" a ndroid:onclick= "oncolorclicked" android:tag= "#99666666"/> <imageview android:layout_wi Dth= "0dip" android:layout_height= "match_parent" android:layout_margin= "4dip" Android:layo ut_weight= "1" android:background= "#9996AA39" android:onclick= "oncolorclicked" Android:tag      = "#9996AA39"/>  <imageview android:layout_width= "0dip" android:layout_height= "Match_parent" Android: Layout_margin= "4dip" android:layout_weight= "1" android:background= "#99C74B46" Android:onc             lick= "oncolorclicked" android:tag= "#99C74B46"/> <imageview android:layout_width= "0dip" android:layout_height= "Match_parent" android:layout_margin= "4dip" android:layout_weight= "1" android:background= "#99F4842D" android:onclick= "oncolorclicked" android:tag= "#99F4842            D "/> <imageview android:layout_width=" 0dip "android:layout_height=" Match_parent "            Android:layout_margin= "4dip" android:layout_weight= "1" android:background= "#993F9FE0" android:onclick= "oncolorclicked" android:tag= "#993F9FE0"/> <imageview android:layou          T_width= "0dip"  android:layout_height= "Match_parent" android:layout_margin= "4dip" android:layout_weight= "1"    Android:background= "#995161BC" android:onclick= "oncolorclicked" android:tag= "#995161BC"/> </LinearLayout></RelativeLayout>
(2) Activity code:

Package Com.sinatj.colorgradientanim;import Android.graphics.color;import android.graphics.drawable.ColorDrawable ; Import Android.graphics.drawable.drawable;import Android.graphics.drawable.layerdrawable;import Android.graphics.drawable.transitiondrawable;import Android.os.build;import Android.os.handler;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.view;import    Android.widget.imageview;public class Mainactivity extends Actionbaractivity {private ImageView ImageView;    Private drawable oldbackground = null;    Private drawable bgdrawable;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        ImageView = (ImageView) Findviewbyid (R.ID.COLOR_IV);        bgdrawable = Getresources (). getdrawable (r.drawable.image_bg_1);    Initial color ChangeColor (Color.parsecolor ("#6696AA39"));  } private void ChangeColor (int newcolor) {      drawable colordrawable = new colordrawable (Newcolor);        Layer Overlay Layerdrawable ld = new Layerdrawable (new drawable[]{bgdrawable, colordrawable});        if (Oldbackground = = null) {imageview.setbackgrounddrawable (LD);            } else {//gradient switch transitiondrawable td = New Transitiondrawable (new Drawable[]{oldbackground, LD});            Imageview.setbackgrounddrawable (TD);        Td.starttransition (300);    } oldbackground = ld;        public void oncolorclicked (View v) {int color = Color.parsecolor (V.gettag (). toString ());    ChangeColor (color); }}



Android: Picture background gradient toggle and layer overlay implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.