1.clipdrawable.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="horizontal"
android:gravity="center">
</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="com.example.qd.clipdrawable.MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/clipdrawable"
/>
</RelativeLayout>
3.代码实现
public class MainActivity extends AppCompatActivity {
private Timer mTimer;
private Handler mHandler;
private ImageView mImageView;
private ClipDrawable mClipDrawable;
private final int FLAG=9527;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
mImageView=(ImageView) findViewById(R.id.imageView);
mClipDrawable=(ClipDrawable) mImageView.getDrawable();
mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==FLAG) {
//不断修改Level值,达到徐徐展开的效果
mClipDrawable.setLevel(mClipDrawable.getLevel()+500);
}
}
};
mTimer=new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Message message=new Message();
message.what=FLAG;
mHandler.sendMessage(message);
if (mClipDrawable.getLevel()>=10000) {
mTimer.cancel();
}
}
}, 0, 300);
}
}
4.运行效果图