我在Android工作室中构建了一个应用程序,我完成了所有逻辑部分,但仍然有一些烦人的东西。
在我的应用中,我已通过
在布局中定义了背景图片node_modules
并点击按钮并使用以下代码更改背景:
android:background="@drawable/firstimg"
背景真的发生了变化,但问题在于背景是如何变化的。
我需要用动画更改背景图片。
我无法使用“imageSwitcher”,因为我在mainLayout中定义了背景图片,我试图通过转换来实现它 - 但它卡住应用程序并继续非常慢......我搜索并没有找到答案。我会很高兴在这里得到一些帮助。
答案 0 :(得分:0)
您可以在代码中使用clip
。
试试这样。
<强> clip_drawable.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal|vertical"
android:drawable="@drawable/firstimg"
android:gravity="center_horizontal|center_vertical">
</clip>
<强> activity_main 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_centerHorizontal="true"
android:padding="0dp"
android:scaleType="fitXY"
android:src="@drawable/clip_drawable" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/iv"
android:layout_marginTop="3dp"
android:gravity="center"
android:onClick="setImage"
android:text="set Image" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn"
android:layout_marginTop="3dp"
android:gravity="center"
android:onClick="changeImage"
android:text="change Image" />
</RelativeLayout>
<强> MainActivity 强>
public class MainActivity extends AppCompatActivity {
private static final int CHANGE_LEVEL = 99;
private ImageView iv;
private ClipDrawable mClipDrawable;
private boolean isExpand = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
mClipDrawable = (ClipDrawable) iv.getDrawable();
mClipDrawable.setLevel(10);
}
public void setImage(View view) {
if (!isExpand) {
isExpand = true;
final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == CHANGE_LEVEL) {
int level = mClipDrawable.getLevel() + 50;
if (level >= 10000) {
level = 10000;
}
mClipDrawable.setLevel(level);
}
}
};
final CountDownTimer timer = new CountDownTimer(Integer.MAX_VALUE, 10) {
@Override
public void onTick(long millisUntilFinished) {
if (mClipDrawable.getLevel() >= 10000) {
this.onFinish();
} else {
mHandler.sendEmptyMessage(99);
}
}
@Override
public void onFinish() {
isExpand = false;
}
};
timer.start();
}
}
public void changeImage(View view) {
if (iv.getDrawable() != null) {
iv.setBackground(iv.getDrawable());
}
// change image here
final ClipDrawable imageDrawable = new ClipDrawable(getResources().getDrawable(R.drawable.third), Gravity.TOP | Gravity.START, ClipDrawable.VERTICAL);
iv.setImageDrawable(imageDrawable);
imageDrawable.setLevel(10);
if (!isExpand) {
isExpand = true;
final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == CHANGE_LEVEL) {
int level = imageDrawable.getLevel() + 50;
if (level >= 10000) {
level = 10000;
}
imageDrawable.setLevel(level);
}
}
};
final CountDownTimer timer = new CountDownTimer(Integer.MAX_VALUE, 10) {
@Override
public void onTick(long millisUntilFinished) {
if (imageDrawable.getLevel() >= 10000) {
this.onFinish();
} else {
mHandler.sendEmptyMessage(99);
}
}
@Override
public void onFinish() {
isExpand = false;
}
};
timer.start();
}
}
}