借助ClipDrawable实现,每隔一段时间截取一块区域,截取的区域不断扩大。
/res/drawable/expanding_picture.xml:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/bg_kou_bei"
android:clipOrientation="horizontal"
android:gravity="">
</clip>
java文件:
package com.example.drawablesource;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
public class ExpandingPictureActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expanding_picture);
ImageView imageView = findViewById(R.id.img_expanding);
final ClipDrawable clipDrawable = (ClipDrawable) imageView.getDrawable();
class MyHandler extends Handler{
@Override
public void handleMessage(@NonNull Message msg) {
if(msg.what == 0x1111){
clipDrawable.setLevel(clipDrawable.getLevel()+200);
}
}
}
final MyHandler handler = new MyHandler();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 0x1111;
handler.sendMessage(message);
if (clipDrawable.getLevel() >=10000){
timer.cancel();
}
}
}, 0, 300);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ExpandingPictureActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_expanding"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@drawable/expanding_picture"/>
</androidx.constraintlayout.widget.ConstraintLayout>