Android中图片属性,ImageView之tint属性

本文介绍了Android中ImageView的tint属性,用于改变图片颜色。通过示例代码展示了如何在布局文件和代码中设置tint,以及如何利用tint实现类似背景选择器的效果,减少应用程序大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天偶尔看到imageview的这个属性,觉得挺好的一个属性,我怎么现在才注意到这个呢?这个属性的作用是改变imageview中作为src的图片的颜色,在布局文件中是android:tint,在代码中对应的设置方法是imageview.setColorFilter(Color),从方法名中就可以看出就是给imageview添加一层遮罩。

看看效果图:

原始界面

0818b9ca8b590ca3270a3433284dd417.png

点击按钮之后

0818b9ca8b590ca3270a3433284dd417.png

再次点击

0818b9ca8b590ca3270a3433284dd417.png

上代码:

新建一个项目TintDemo

布局文件fragment_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.tintdemo.MainActivity$PlaceholderFragment" >

android:id="@+id/btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Tint演示" />

android:id="@+id/img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:contentDescription="演示"

android:src="@drawable/ic_launcher" />

MainActivity

package com.example.tintdemo;

import android.app.Notification.Action;

import android.graphics.Color;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;

import android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

private Button btn;

private ImageView img;

private Type type;

enum Type {

DarkGray, NULL

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.fragment_main);

type = Type.NULL;// 初始化img属性设置为空

init();

}

/**

* 初始化控件

*/

private void init() {

// TODO Auto-generated method stub

btn = (Button) findViewById(R.id.btn);

img = (ImageView) findViewById(R.id.img);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

setFilter(type, img);

}

});

// 实现类似被背景选择器的功能

img.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

img.setColorFilter(Color.DKGRAY);

break;

case MotionEvent.ACTION_UP:

img.setColorFilter(null);

break;

}

return true;

}

});

}

private void setFilter(Type type, ImageView img) {

switch (type) {

case DarkGray:

this.type = Type.NULL;

img.setColorFilter(null);

break;

case NULL:

this.type = Type.DarkGray;

img.setColorFilter(Color.DKGRAY);

break;

}

}

}上面写的代码是用代码实现tint属性,也可以在布局文件中实现,很简单,代码如下

android:id="@+id/img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:contentDescription="演示"

android:src="@drawable/ic_launcher"

android:tint="@android:color/darker_gray" />相比原始的布局文件就多了一句代码

android:tint="@android:color/darker_gray"这样的话在程序初始化的时候我们的图标颜色就是暗灰色了。

注意下面这一段代码,记得我们常用的背景选择器吗?很多时候我是用对一张原始图片作灰度值处理之后与原始图片一起制作一个背景选择器,现在不用了,只用tint就可以实现,这样的话就减小了应用程序的大小。

// 实现类似被背景选择器的功能

img.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

img.setColorFilter(Color.DKGRAY);

break;

case MotionEvent.ACTION_UP:

img.setColorFilter(null);

break;

}

return true;

}

});

我不会截取动态图,这个效果就不演示了,自己写一下试试吧?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值