我正在尝试运行一个简单的应用程序,该应用程序在单击按钮时显示带有动画的图像。我看到的只是一个白屏。我使用的是教程中的确切代码。有人能告诉我什么问题吗?这是教程https://www.tutorialspoint.com/android/android_imageswitcher.htm
这是 MainActivity.java
import...
public class MainActivity extends AppCompatActivity {
ImageSwitcher imageSwitcher;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher=(ImageSwitcher)findViewById(R.id.img);
btn=(Button)findViewById(R.id.button);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView view = new ImageView(getApplicationContext());
view.setScaleType(ImageView.ScaleType.CENTER);
view.setLayoutParams(new
ImageSwitcher.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT));
return view;
}
});
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "s", Toast.LENGTH_SHORT).show();
imageSwitcher.setImageResource(R.drawable.m1);
}
});
}
}
这是我的 xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="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="vaibhav.com.game.MainActivity">
<ImageSwitcher
android:id="@+id/img"
android:layout_width="324dp"
android:layout_height="419dp"
android:layout_margin="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="57dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.439"
app:layout_constraintVertical_bias="0.946" />
</android.support.constraint.ConstraintLayout>