利用ClipDrawable实现照片的徐徐展开
MainActivity如下:
package cc.testclipdrawable;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.widget.ImageView;import android.app.Activity;import android.graphics.drawable.ClipDrawable;/** * Demo描述: * 利用ClipDrawable实现照片的徐徐展开 * * 示例说明: * 1 在clipdrawable中使用clip来描述一张将被徐徐展开的图片 * android:drawable 设定图片资源 * android:clipOrientation 设定clip的方向 * android:gravity 设定clip时的对齐方式 * * 2 ClipDrawable对象可以调用setLevel()方法来设置clip区域的大小 * 其中0表示截取原图的大小为空,10000表示截取整张图片 * * 参考资料: * 疯狂Android讲义(第二版) 作者 李刚 * */public class MainActivity extends Activity {private Timer mTimer;private Handler mHandler; private ImageView mImageView; private ClipDrawable mClipDrawable; private final int FLAG=9527;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){mImageView=(ImageView) findViewById(R.id.imageView);mClipDrawable=(ClipDrawable) mImageView.getDrawable();mHandler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (msg.what==FLAG) {//不断修改Level值,达到徐徐展开的效果mClipDrawable.setLevel(mClipDrawable.getLevel()+500);}}};mTimer=new Timer();mTimer.schedule(new TimerTask() {@Overridepublic void run() {Message message=new Message();message.what=FLAG;mHandler.sendMessage(message);if (mClipDrawable.getLevel()>=10000) {mTimer.cancel();}}}, 0, 300);}}
main.xml如下:
<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" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/clipdrawable" /></RelativeLayout>
clipdrawable如下:
<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/beauty" android:clipOrientation="horizontal" android:gravity="center"></clip>