Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果
本文實(shí)例講述了Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果。分享給大家供大家參考,具體如下:
本例要實(shí)現(xiàn)的目的:
1.圖片背景漸變的切換,例如漸變的從紅色切換成綠色。
2.代碼中進(jìn)行圖層疊加,即把多個(gè)Drawable疊加在一起顯示在一個(gè)組件之上。
效果圖:

代碼很簡(jiǎn)單:
(1)布局文件:
<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:paddingLeft="@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:layout_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="點(diǎn)擊顏色塊,切換圖片背景" />
<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"
android:onClick="onColorClicked"
android:tag="#99666666" />
<ImageView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_margin="4dip"
android:layout_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:onClick="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="#99F4842D" />
<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:layout_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代碼:
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);
//初始顏色
changeColor(Color.parseColor("#6696AA39"));
}
private void changeColor(int newColor) {
Drawable colorDrawable = new ColorDrawable(newColor);
//圖層疊加
LayerDrawable ld = new LayerDrawable(new Drawable[]{bgDrawable, colorDrawable});
if (oldBackground == null) {
imageView.setBackgroundDrawable(ld);
} else {
//漸變切換
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);
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動(dòng)切換效果
- Android自定義ImageView實(shí)現(xiàn)點(diǎn)擊兩張圖片切換效果
- Android點(diǎn)擊Button實(shí)現(xiàn)切換點(diǎn)擊圖片效果的示例
- Android編程單擊圖片實(shí)現(xiàn)切換效果的方法
- Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片
- Android中ViewPager組件的基本用法及實(shí)現(xiàn)圖片切換的示例
- Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼
- Android 圖片切換器(dp、sp、px) 的單位轉(zhuǎn)換器
- Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
- Android實(shí)現(xiàn)左右滑動(dòng)切換圖片
相關(guān)文章
Android 超簡(jiǎn)易Zxing框架 生成二維碼+掃碼功能
這篇文章主要介紹了Android 超簡(jiǎn)易Zxing框架 生成二維碼+掃碼功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
讓Android應(yīng)用不被殺死(killer)的方法
這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實(shí)現(xiàn)方法和原理分析,需要的朋友可以參考下2015-04-04
RecyclerView使用payload實(shí)現(xiàn)局部刷新
這篇文章主要為大家詳細(xì)介紹了RecyclerView使用payload實(shí)現(xiàn)局部刷新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
解決Android Studio Gradle Metadata特別慢的問(wèn)題
這篇文章主要介紹了解決Android Studio Gradle Metadata特別慢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android編程自定義菜單實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android編程自定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android自定義菜單的布局、動(dòng)畫(huà)及功能相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-02-02
Android仿微信Viewpager-Fragment惰性加載(lazy-loading)
這篇文章主要為大家詳細(xì)介紹了Android仿微信Viewpager-Fragment惰性加載lazy-loading,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android使用Jetpack WindowManager開(kāi)發(fā)可折疊設(shè)備(過(guò)程分享)
這篇文章主要介紹了Android使用Jetpack WindowManager開(kāi)發(fā)可折疊設(shè)備,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
android閃關(guān)燈的開(kāi)啟和關(guān)閉方法代碼實(shí)例
這篇文章主要介紹了android閃關(guān)燈的開(kāi)啟和關(guān)閉方法代碼實(shí)例,本文直接給出代碼和配置實(shí)例,需要的朋友可以參考下2015-05-05
android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

