使用ClipDrawable实现图片徐徐展开效果
ClipDrawable 代表从其他位图上截取一个”图片片段” 调用 setLevel(int level) 方法来设置截取的区域大小 当level为0时,截取的图片片段为空; 当level为10000时,截取整张图片。 一开始level默认为0。
以下为源代码
在drawable文件下定义一个xml文件
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/aaa" <!--指定截取源文件-->
android:clipOrientation="horizontal"
android:gravity="center"
>
</clip>
布局文件
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.example.brazen.test.MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/clipdrawable"/>
</LinearLayout>
Java代码
public class MainActivity extends Activity {
Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
ImageView imageview = (ImageView) findViewById(R.id.imageview);
final ClipDrawable clipDrawable = (ClipDrawable) imageview.getDrawable();
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
clipDrawable.setLevel(clipDrawable.getLevel() + 200);
}
};
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (clipDrawable.getLevel() < 10000)
timer.cancel();
mHandler.sendEmptyMessage(0x123);
}
}, 0, 300);
}
}
运行效果图