Android ImageView的Tint属性

Android Tint使用


Tint 属性

  Tint 是 Android5.0 引入的一个属性,它可以在Android5.0 系统上,对视图进行颜色渲染。 
下面是网上一个使用tint属性给背景调整不同颜色的例子:

 <LinearLayout  
        android:orientation="horizontal"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center_horizontal">  
        <ImageView  
            ...  
            android:src="@drawable/xamarin_white"  
            android:background="@drawable/mycircle"/>  
        <ImageView  
            ...  
            android:src="@drawable/xamarin_white"  
            android:background="@drawable/mycircle"  
            android:tint="#2C3E50"/>  
        <ImageView  
            ...  
            android:src="@drawable/xamarin_white"  
            android:background="@drawable/mycircle"  
            android:tint="#B4BCBC"/>  
    </LinearLayout>  

效果图: 
此处输入图片的描述

  tint这个属性,是ImageView有的,它可以给ImageView的src设置,除了tint 之外,还有backgroundTint,foregroundTint,drawableTint,它们分别对应对背景、前景、drawable进行着色处理。 如果,我们给上面的例子设置 backgroundTint,那么蓝色背景就会被着色,替换成你设置的颜色。

原理

  在5.0以上,View类中增加了对tint属性的获取

  case R.styleable.View_backgroundTint:
                    // This will get applied later during setBackground().
                    if (mBackgroundTint == null) {
                        mBackgroundTint = new TintInfo();
                    }
                    mBackgroundTint.mTintList = a.getColorStateList(
                            R.styleable.View_backgroundTint);
                    mBackgroundTint.mHasTintList = true;
                    break;
case R.styleable.View_backgroundTintMode:
    // This will get applied later during setBackground().
    if (mBackgroundTint == null) {
        mBackgroundTint = new TintInfo();
    }
    mBackgroundTint.mTintMode = Drawable.parseTintMode(a.getInt(
            R.styleable.View_backgroundTintMode, -1), null);
    mBackgroundTint.mHasTintMode = true;
    break;

以具体的 ImageView 为例

//在构造函数中调用了这个方法
private void applyImageTint() {
        if (mDrawable != null && (mHasDrawableTint || mHasDrawableTintMode)) {
            mDrawable = mDrawable.mutate();

            if (mHasDrawableTint) {
                mDrawable.setTintList(mDrawableTintList);
            }

            if (mHasDrawableTintMode) {
                mDrawable.setTintMode(mDrawableTintMode);
            }

            // The drawable (or one of its children) may not have been
            // stateful before applying the tint, so let's try again.
            if (mDrawable.isStateful()) {
                mDrawable.setState(getDrawableState());
            }
        }

setTintList 方法的实现

public void setTintList(ColorStateList tint) {
        mBitmapState.mTint = tint;
        mTintFilter = updateTintFilter(mTintFilter, tint, mBitmapState.mTintMode);
        invalidateSelf();
    }

当追踪到 updateTintFilter()这个方法的时候,我们就无法继续向下追踪了,不过研究参数也可以得出它实现的方式(PorterDuffColorFilter,BitmapDrawable.BitmapState,PorterDuff.Mode), 很明显,还是利用的PorterDuff那些相关类来实现的操作,ProterDuff 网上也有很多说明的例子,最重要的还是下图: 有关这些Mode的详细解释,大家自行查阅 
此处输入图片的描述

版本问题

Tint在默认只在Android5.0以上的系统生效,为了向下支持,系统提供了相应的Compact类,包括 
AppCompatTextView、AppCompatImageView等。我们使用 ViewCompat.setBackgroundTintMode 在懂吗中动态的为 View 进行tint 操作,观察 这些Compat类发现都实现了 TintableBackgroundView 这个接口,如果需要让自定义的 View实现tint功能,我们可以仿照系统的实现类来实现。

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.v4.view;

import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.support.annotation.Nullable;

/**
 * Interface which allows a {@link android.view.View} to receive background tinting calls from
 * {@code ViewCompat} when running on API v20 devices or lower.
 */
public interface TintableBackgroundView {

    /**
     * Applies a tint to the background drawable. Does not modify the current tint
     * mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
     * <p>
     * Subsequent calls to {@code View.setBackground(Drawable)} will automatically
     * mutate the drawable and apply the specified tint and tint mode.
     *
     * @param tint the tint to apply, may be {@code null} to clear tint
     *
     * @see #getSupportBackgroundTintList()
     */
    void setSupportBackgroundTintList(@Nullable ColorStateList tint);

    /**
     * Return the tint applied to the background drawable, if specified.
     *
     * @return the tint applied to the background drawable
     */
    @Nullable
    ColorStateList getSupportBackgroundTintList();

    /**
     * Specifies the blending mode used to apply the tint specified by
     * {@link #setSupportBackgroundTintList(ColorStateList)}} to the background
     * drawable. The default mode is {@link PorterDuff.Mode#SRC_IN}.
     *
     * @param tintMode the blending mode used to apply the tint, may be
     *                 {@code null} to clear tint
     * @see #getSupportBackgroundTintMode()
     */
    void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode);

    /**
     * Return the blending mode used to apply the tint to the background
     * drawable, if specified.
     *
     * @return the blending mode used to apply the tint to the background
     *         drawable
     */
    @Nullable
    PorterDuff.Mode getSupportBackgroundTintMode();
}

参考:http://www.open-open.com/lib/view/open1454052058573.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ImageViewAndroid Studio中的一个视图控件,用于显示图片。它有多种属性,包括: 1. src:设置ImageView显示的图片资源,可以是本地图片或网络图片的URL。 2. scaleType:设置图片的缩放方式,包括fitXY、centerCrop、centerInside等。 3. adjustViewBounds:设置ImageView的大小是否根据图片的大小自动调整。 4. maxWidth和maxHeight:设置ImageView的最大宽度和最大高度。 5. tint:设置图片的颜色过滤器,可以改变图片的颜色。 6. alpha:设置图片的透明度。 7. visibility:设置ImageView的可见性,可以是visible、invisible或gone。 8. background:设置ImageView的背景颜色或背景图片。 以上是ImageView的一些常用属性,可以根据实际需要进行设置。 ### 回答2: Android Studio是一款流行的Android应用程序开发工具,在开发Android应用程序时,ImageView是其中的一个重要组件。下面将介绍ImageView属性ImageView属性主要包括图片源、缩放类型和内边距。 图片源:ImageView的src属性定义了ImageView的图片源。该属性的值可以是Drawable类型,也可以是通过资源ID直接引用的图像资源。在设置图像时,我们可以使用向下兼容性。例如,我们可以通过在主程序包中存储图像来向下兼容。 缩放类型:ImageView的scaleType属性定义了图像应该如何在ImageView中缩放。该属性的值可以是下列缩放类型之一: - CENTER:将图像居中,不进行缩放。 - CENTER_CROP:缩放图像使其填满整个ImageView,但是保留图像的宽高比。如果图像尺寸与ImageView尺寸不匹配,则会裁剪图像。 - CENTER_INSIDE:缩放图像使其适应ImageView的尺寸。如果图像尺寸与ImageView尺寸不同,则图像仅在它们共享相同尺寸的范围内显示。 - FIT_CENTER:缩放图像让其适应ImageView大小,并保留宽高比。如果图像尺寸与ImageView尺寸不同,则在图像之间留出空白。 - FIT_XY:缩放图像拉伸或压缩图像,使其填满整个ImageView区域。不保留原始宽高比。 - FIT_END:在 ImageView 右下角放置图像,保留图像的宽高比。如果图像尺寸不等于 ImageView 尺寸,则放置图像的边缘放置在 ImageView 的边缘上。 - FIT_START:在 ImageView 左上角放置图像,保留图像的宽高比。如果图像尺寸不等于 ImageView 尺寸,则放置图像的边缘放置在 ImageView 的边缘上。 - FIT_BOTTOM_START:在 ImageView 左下角放置图像,保留图像的宽高比。如果图像尺寸不等于 ImageView 尺寸,则放置图像的边缘放置在 ImageView 的边缘上。 内边距:ImageView的padding属性为图像提供了一个内边距。如果我们需要在图像周围创建空白,我们可以使用它。 另外,ImageView还有一些其他的属性,如layout_width和layout_height,用于定义视图在父容器中的大小。除此之外,还有一些自定义属性,让我们可以进一步自定义ImageView的表现方式。 ### 回答3: Android Studio是当前最流行的Android开发工具,ImageView是其中的一种常用的视图控件,用于展示图片。 ImageView属性包括以下几种: 1. src属性:指定ImageView所要展示的图片资源的路径。 2. scaleType属性:图片展示的缩放方式,可选的值包括fitXY、centerCrop、centerInside等。 3. adjustViewBounds属性:可用于控制ImageView大小的自适应调整,当ImageView的大小大于图片的大小时,ImageView会根据adjustViewBounds属性值进行大小调整。 4. maxHeight和maxWidth属性:可用于控制ImageView显示图片的最大高度和宽度。 5. alpha属性:图片的透明度,值为0-1之间的浮点数。 6. drawable属性:可用于在代码中为ImageView设置图片,为此需要创建Drawable对象。 除此之外,还有其他的一些属性,例如padding、background等,都可以通过Android Studio的属性面板进行设置。 总的来说,ImageViewAndroid开发中比较基础的一个控件,常用于展示图片,其属性也相对简单易懂,学习掌握后可以高效地完成图片相关的布局和设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值