您的位置:首页 > 其它

ClipDrawable的使用

2016-04-24 14:33 309 查看
ClipDrawable的使用

ClipDrawable对应于标签,他可以根据自己当前的等级(level)来裁剪另一个drawable。裁剪的方向可以通过android:clipOrientation和android:gravity属性来共同控制。

例子:

1.在clip_drawable.xml文件中

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/ic_launcher"
android:clipOrientation="vertical"
android:gravity="top">

<!--clipOrientation:有水平和竖直方向-->
<!--gravity:属性比较多。top,bottom,left,right,center,center_vertical,center_horizontal等。-->
</clip>


2.在activity_main.xml文件中

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="www.yundo.com.drawable.MainActivity">

<ImageView
android:id="@+id/iv"
android:src="@drawable/clip_drawable"
android:background="@color/red"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
</RelativeLayout>


3.在MainActivity文件中

public class MainActivity extends AppCompatActivity {

private ImageView mImageView;
private boolean isBlueToRed;
private TransitionDrawable tranBackground;
private ClipDrawable clipBackground;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mImageView = (ImageView) findViewById(R.id.iv);

clipBackground = (ClipDrawable) mImageView.getDrawable();

mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clip();
}
});
}
private void clip() {
//level在0-10000之间,0表示完全裁剪,10000表示完全不裁剪。设置的值越大,裁剪的范围就越小。
clipBackground.setLevel(5000);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: