运行结果
MainActivity.java
实现了三种效果,旋转,改变大小以及背景颜色
public class MainActivity extends AppCompatActivity {
private Button button2,button1,button3;
private boolean a=true,b=true;
private float xuanzhuan=0;
private ImageView imageView3,imageView1,imageView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button3=(Button) findViewById(R.id.button3);
imageView1=(ImageView) findViewById(R.id.imageView1);
imageView2=(ImageView) findViewById(R.id.imageView2);
imageView3=(ImageView) findViewById(R.id.imageView3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
xuanzhuan+=20;
imageView1.setRotation(xuanzhuan);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
a=!a;
if(!a){
imageView3.setScaleX(2);
imageView3.setScaleY(2);
}
else{
imageView3.setScaleX(1);
imageView3.setScaleY(1);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
b=!b;
if(!b) imageView2.setBackgroundColor(Color.rgb(255,248,220));
else imageView2.setBackgroundColor(Color.rgb(255,255,255));
}
});
}
}
activity_main.xml
<?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=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="410dp"
android:layout_height="189dp"
android:background="@color/green"
android:rotation="0"
android:rotationX="0"
android:src="@drawable/huawei_logl"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="346dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.874"
app:srcCompat="@drawable/girl" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="281dp"
android:layout_height="168dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06"
app:srcCompat="@drawable/girl" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.673" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变大小"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.295" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换颜色"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976" />
</androidx.constraintlayout.widget.ConstraintLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="green">#b6d7a8</color>
</resources>