AndroidのResource - ClipDrawableサンプルコード
① my_clip.xml
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/skytree" android:clipOrientation="horizontal" android:gravity="center"> </clip>
② activity_main2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitStart" android:src="@drawable/my_clip" /> </LinearLayout>
③ MainActivity.java
package com.example.liguofeng.drawablesample; import android.graphics.drawable.ClipDrawable; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import java.util.Timer; import java.util.TimerTask; import android.os.Handler; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ImageView imageview = (ImageView) findViewById(R.id.image); // ClipDrawable取得 final ClipDrawable drawable = (ClipDrawable) imageview.getDrawable(); final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // メッセージ取得 if (msg.what == 0x1233) { // ClipDrawableのlevel変更 drawable.setLevel(drawable.getLevel() + 200); } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Message msg = new Message(); msg.what = 0x1233; // メッセージ送信 handler.sendMessage(msg); if (drawable.getLevel() >= 10000) { // タイマー取り消し timer.cancel(); } } }, 0, 300); } }