Drawable日常使用简识(3)----ClipDrawable的基本使用

Drawable是对可绘制资源的一种抽象,他和view不同,它不具有可交互性。在我们的项目结构中,通常在res下面有一个名为drawable的文件夹,里面的资源我们是可以通过以下两种方式获取:

(1)R.drawable.xxx

(2)getResources().getDrawable(R.drawable.xxx)

      接下来我就会对常用的Drawable(本身是一个抽象类)子类的使用做一个简单的介绍,今天主要介绍一下ClipDrawable,在看之前也可以看一下我的上两篇关于BitmapDrawable基本使用LayerDrawable基本使用的博客

1,ClipDrawable

它主要用于控制其他Drawable的显示比例,也就是我们常常说的剪切,官方给出的解释如下:

A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars.

It can be defined in an XML file with the <clip> element.

一个Drawable根据当前的level值去剪切另一个Drawable,你可以通过level值控制子Drawable的剪切比例(宽和高),也可以控制它在整个容器中的布局方向;通常用于实现像进度条这样的效果

可以通过在xml文件中用<clip>标签来定义

扫描二维码关注公众号,回复: 2948794 查看本文章

构造方法

ClipDrawable(Drawable drawable, int gravity, int orientation)

接口方法:

 void draw(Canvas canvas)
          Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
 int getChangingConfigurations()
          Return a mask of the configuration parameters for which this drawable mau change, requiring that it be re-created.
 Drawable.ConstantState getConstantState()
           
 int getIntrinsicHeight()
          Return the intrinsic height of the underlying drawable object.
 int getIntrinsicWidth()
          Return the intrinsic width of the underlying drawable object.
 int getOpacity()
          Return the opacity/transparency of this Drawable.
 boolean getPadding(Rect padding)
          Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.
 void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
           
 void invalidateDrawable(Drawable who)
          Called when the drawable needs to be redrawn.
 boolean isStateful()
          Indicates whether this view will change its appearance based on state.
 void scheduleDrawable(Drawable who, Runnable what, long when)
          A Drawable can call this to schedule the next frame of its animation.
 void setAlpha(int alpha)
          Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.
 void setColorFilter(ColorFilter cf)
          Specify an optional colorFilter for the drawable.
 boolean setVisible(boolean visible, boolean restart)
          Set whether this Drawable is visible.
 void unscheduleDrawable(Drawable who, Runnable what)
          A Drawable can call this to unschedule an action previously scheduled with Drawable.Callback.scheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable, long).

2,基本使用

2.1 定义ClipDrawable对应的xml文件

clip_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/face_man"
    android:clipOrientation="horizontal"
    android:gravity="left">

</clip>

其中给出了剪切方向属性,布局属性等

2.2 编写测试代码

ClipDrawableActivity代码:

package com.hfut.operationdrawable;

import android.graphics.drawable.ClipDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * @author why
 * @date 2018-8-13 18:35:07
 */
public class ClipDrawableActivity extends AppCompatActivity {

    private static final String TAG = "ClipDrawableActivity";
    ImageView imageView;
    EditText clipPercent;
    ClipDrawable clipDrawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clip_drawable);
        imageView=findViewById(R.id.clip_view);
        clipPercent=findViewById(R.id.clip_percent);
        clipDrawable= (ClipDrawable) imageView.getDrawable();
        clipDrawable.setLevel(10000);
    }
    
    public void clipImage(View view){
        //没有做非空判断
        float percent=Float.parseFloat(clipPercent.getText().toString());
        imageView.setImageDrawable(clipDrawable);
        clipDrawable.setLevel((int)((1.0-percent)*10000));
        Log.d(TAG, "clipImage: "+(int)(percent*10000));
    }
}

activity_clip_drawable.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.hfut.operationdrawable.ClipDrawableActivity">

    <ImageView
        android:id="@+id/clip_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:src="@drawable/clip_image" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <EditText
            android:id="@+id/clip_percent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="水平剪切比例(0.0-1.0)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="clipImage"
            android:text="剪切" />

    </LinearLayout>

</LinearLayout>

2.3 测试结果

第一步:运行程序

第二步:输入剪切比例

3,注意事项

(1)剪切的level值在0-10000之间,0表示全剪切,10000表示全显示

clipDrawable.setLevel(10000);

源码:

/**
 * @param level The new level, from 0 (minimum) to 10000 (maximum).
 */
public final boolean setLevel(@IntRange(from=0,to=10000) int level) {
    if (mLevel != level) {
        mLevel = level;
        return onLevelChange(level);
    }
    return false;
}

(2)我们这里的ClipDrawable对象是从ImageView中获取的,因为我们要对其进行剪切,而不是通过资源xml文件获取

正确:

clipDrawable= (ClipDrawable) imageView.getDrawable();

错误:

clipDrawable= (ClipDrawable) getResources().getDrawable(R.drawable.clip_image);

猜你喜欢

转载自blog.csdn.net/hfut_why/article/details/81937425