欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實現(xiàn)圖片背景漸變切換與圖層疊加效果

 更新時間:2017年01月25日 11:02:08   作者:books1958  
這篇文章主要介紹了Android編程實現(xiàn)圖片背景漸變切換與圖層疊加效果,涉及Android圖形特效的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Android編程實現(xiàn)圖片背景漸變切換與圖層疊加效果。分享給大家供大家參考,具體如下:

本例要實現(xiàn)的目的:

1.圖片背景漸變的切換,例如漸變的從紅色切換成綠色。

2.代碼中進行圖層疊加,即把多個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);
  }
}

更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • 詳解利用Flutter中的Canvas繪制有趣的圖形

    詳解利用Flutter中的Canvas繪制有趣的圖形

    本文將利用Flutter中的Canvas繪制三個有趣的圖形:使用等邊三角形組合成彩虹傘面、五角星和彩虹,快來跟隨小編一起動手嘗試一下吧
    2022-03-03
  • Android中Canvas的常用方法總結(jié)

    Android中Canvas的常用方法總結(jié)

    在Android自定義View的時候,我們經(jīng)常需要繪制一些自己想要的效果。這里就需要使用Canvas對象。下面這篇文章將Canvas對象常用方法做個筆記,方便自己和大家以后使用的時候查閱,下面來一起看看吧。
    2016-09-09
  • 利用Kotlin的方式如何處理網(wǎng)絡異常詳解

    利用Kotlin的方式如何處理網(wǎng)絡異常詳解

    這篇文章主要 給大家介紹了關于利用Kotlin的方式如何處理網(wǎng)絡異常的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • Android 仿京東秒殺倒計時代碼

    Android 仿京東秒殺倒計時代碼

    3.8女王節(jié)活動馬上開始了,很多電商搞起活動來吸引顧客,下面小編給大家?guī)砹薃ndroid 仿京東秒殺倒計時代碼,需要的朋友參考下吧
    2018-03-03
  • Android自定義狀態(tài)欄顏色與應用標題欄顏色一致

    Android自定義狀態(tài)欄顏色與應用標題欄顏色一致

    看IOS上的應用,應用中狀態(tài)欄的顏色總能與應用標題欄顏色保持一致,用戶體驗很不錯,對于這種效果怎么實現(xiàn)的呢?下面小編給大家分享android自定義狀態(tài)欄顏色與應用標題欄顏色一致的實現(xiàn)方法,一起看看吧
    2016-09-09
  • Android實現(xiàn)圖片壓縮(bitmap的六種壓縮方式)

    Android實現(xiàn)圖片壓縮(bitmap的六種壓縮方式)

    Android中圖片是以bitmap形式存在的,這篇文章主要介紹了Android實現(xiàn)圖片壓縮(bitmap的六種壓縮方式),有興趣的可以了解一下。
    2017-02-02
  • Android自定義SurfaceView實現(xiàn)畫板功能

    Android自定義SurfaceView實現(xiàn)畫板功能

    這篇文章主要為大家詳細介紹了Android自定義SurfaceView實現(xiàn)畫板功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android音視頻開發(fā)之MediaCodec的使用教程

    Android音視頻開發(fā)之MediaCodec的使用教程

    在Android開發(fā)中提供了實現(xiàn)音視頻編解碼工具MediaCodec,針對對應音視頻解碼類型通過該類創(chuàng)建對應解碼器就能實現(xiàn)對數(shù)據(jù)進行解碼操作。本文通過示例詳細講解了MediaCodec的使用,需要的可以參考一下
    2022-04-04
  • 解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功)

    解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功)

    這篇文章主要介紹了解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • Android自定義鐘表特效

    Android自定義鐘表特效

    這篇文章主要為大家詳細介紹了Android自定義鐘表特效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12

最新評論