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

代碼很簡單:
(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="點擊顏色塊,切換圖片背景" />
<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開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android開發(fā)之使用ViewPager實現(xiàn)圖片左右滑動切換效果
- Android自定義ImageView實現(xiàn)點擊兩張圖片切換效果
- Android點擊Button實現(xiàn)切換點擊圖片效果的示例
- Android編程單擊圖片實現(xiàn)切換效果的方法
- Android實現(xiàn)滑動屏幕切換圖片
- Android中ViewPager組件的基本用法及實現(xiàn)圖片切換的示例
- Android實現(xiàn)圖片輪播切換實例代碼
- Android 圖片切換器(dp、sp、px) 的單位轉(zhuǎn)換器
- Android控件ImageSwitcher實現(xiàn)左右圖片切換功能
- Android實現(xiàn)左右滑動切換圖片
相關(guān)文章
利用Kotlin的方式如何處理網(wǎng)絡(luò)異常詳解
這篇文章主要 給大家介紹了關(guān)于利用Kotlin的方式如何處理網(wǎng)絡(luò)異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致
看IOS上的應(yīng)用,應(yīng)用中狀態(tài)欄的顏色總能與應(yīng)用標(biāo)題欄顏色保持一致,用戶體驗很不錯,對于這種效果怎么實現(xiàn)的呢?下面小編給大家分享android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致的實現(xiàn)方法,一起看看吧2016-09-09
Android實現(xiàn)圖片壓縮(bitmap的六種壓縮方式)
Android中圖片是以bitmap形式存在的,這篇文章主要介紹了Android實現(xiàn)圖片壓縮(bitmap的六種壓縮方式),有興趣的可以了解一下。2017-02-02
Android自定義SurfaceView實現(xiàn)畫板功能
這篇文章主要為大家詳細(xì)介紹了Android自定義SurfaceView實現(xiàn)畫板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android音視頻開發(fā)之MediaCodec的使用教程
在Android開發(fā)中提供了實現(xiàn)音視頻編解碼工具M(jìn)ediaCodec,針對對應(yīng)音視頻解碼類型通過該類創(chuàng)建對應(yīng)解碼器就能實現(xiàn)對數(shù)據(jù)進(jìn)行解碼操作。本文通過示例詳細(xì)講解了MediaCodec的使用,需要的可以參考一下2022-04-04
解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功)
這篇文章主要介紹了解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

