Android:图片背景渐变切换与图层叠加的实现

本例要实现的目的:

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);
    }
}
时间: 2024-08-29 05:38:17

Android:图片背景渐变切换与图层叠加的实现的相关文章

android 图片浏览器滑动切换图片

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation

ImageButton图片背景的切换

图形按钮:以图形代表文字,并且可以按照按钮的状态,列入,正常.按下(press).聚焦(focus),来显示对应的图形文件,又称做自定义按钮· ImageButton图片的切换有多种现在先来说在xml中的切换 在xml实现动态切换使用selector标签,代码如下 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android=http://schemas.android.com/apk

Android 图片闪烁(延迟切换)

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:o

Tesseract处理背景渐变的图片

在Tesseract处理背景渐变图片不太理想的情况下, 可以利用Pillow库, 创建一个阈值过滤器来去掉渐变的背景色, 只把文字留下来, 从而让图片更清晰, 便于Tesseract读取: 1 from PIL import Image 2 import subprocess 3 4 def cleanFile(filePath, newFilePath): 5 image = Image.open(filePath) 6 7 # 对图片进行阈值过滤, 然后保存 8 image = image.

Android聊天背景图片变形解决方案

Android聊天背景图片变形,一般是由于键盘引起的(这个是自己调戏糖宝app中出现的问题,今天抽时间解决下).可以参看StackOverFlow解决的地址:http://stackoverflow.com/questions/5307264/how-to-prevent-soft-keyboard-from-resizing-background-image 我这里用的也是参考这里的方法: 解决方法很简单: 1.在AndroidManifest.xml文件里面的Activity配置:andro

[Android UI]可自动切换、无限滑动的图片广告展示栏的实现分享(续1)

本文非技术类文章,博主只是想分享这样的一种实现方法,帮助有需要的开发人员减少工作量.我是学java出身的,做开发2年了,很理解一句话:工欲善其事必先利其器. 继另一篇博文:[Android初级]可自动切换.无限滑动的广告栏的实现  ,我觉得还有更好的方式来展示,这个比较合适更多开发人员. 本次实现的demo,用到了第三方的开源框架:1.JazzyViewPager https://github.com/jfeinstein10/JazzyViewPager(主要用它,效果比较多) 2.unive

android XMl Selector 图片背景点击和焦点获取样式

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默认时的背景图片 --> <item android:drawable="@drawable/menu_frame_scale_no_focused"/>

网页背景图片的定时切换

我们在做页面效果的时候,时常会遇到UI为了做出好看的效果,会要求body,或者div的背景定时切换. 如果是要做后台可以增删查改背景图的话,绝对不可以用background来设定背景图,因为这样后台是没办法改掉背景url的路径的. 所以,要做后台可以改图片的话,就应该在html中用img还设定背景图. 最近我在做的是一个用div的背景定时切换的效果. 就是在头部部分,既要做到满屏,又要有定时轮换的效果.因为一开始在做这里的时候,我是用background做的,因为也不做后台该图片的功能,我就依旧

Android selector(背景选择器) , shape(设定形状)

Selector: <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默认时的背景图片--> <item android:drawable="@drawable/pic1" /> <!-- 没有焦