//把圖片保存到数组里
private int imageViewId[] = {R.drawable.z1,R.drawable.z2,R.drawable.z3,R.drawable.z4,R.drawable.s1};
private ImageView iv_main_image;
private int currentIndex = 0;
private int transparent = 255;//图片的清晰度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 首先要获取imageview控件
iv_main_image = findViewById(R.id.iv_main_image);
iv_main_image.setImageResource(imageViewId[currentIndex]);
}
public void last(View view){
if(currentIndex == 0){
Toast.makeText(this,”不能再上了,没人了”,Toast.LENGTH_SHORT).show();
}else {
currentIndex –;
iv_main_image.setImageResource(imageViewId[currentIndex]);
}
}
public void next(View view){
// 点击下一张按钮更改id
currentIndex++;
if(currentIndex >= imageViewId.length){
currentIndex = imageViewId.length -1;
Toast.makeText(this,”目前已经是最后一张了,充钱查看更多”,Toast.LENGTH_SHORT).show();
}else {
// 将更新后的图片id设置会imageview控件
iv_main_image.setImageResource(imageViewId[currentIndex]);
}
}
public void add(View view){
if(transparent<= 235){
transparent = transparent +20;
// 然后再将改变后的值设置会imageview控件
iv_main_image.setImageAlpha(transparent);
}else {
Toast.makeText(this,"目前已经是高清了",Toast.LENGTH_SHORT).show();
}
}
public void del(View view){
// 首先要获取imageview控件
// 默认透明度是255,也就是完全不透的,点击透明按钮,我们要控制透明度的值减小
// System.out.println(“##########################################”+(transparent–));
// System.out.println(“##########################################”+(–transparent));
if(transparent>=20){
transparent = transparent -20;
// 然后再将改变后的值设置会imageview控件
iv_main_image.setImageAlpha(transparent);
}else {
Toast.makeText(this,"再透明就什么都看不见了",Toast.LENGTH_SHORT).show();
}
;
}
//xml
<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content">-->
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:onClick="last"-->
<!--android:text="<"/>-->
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:text="+"-->
<!--android:onClick="add"-->
<!--android:layout_height="wrap_content" />-->
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:text="-"-->
<!--android:onClick="del"-->
<!--android:layout_height="wrap_content" />-->
<!--<Button-->
<!--android:layout_width="wrap_content"-->
<!--android:text=">"-->
<!--android:onClick="next"-->
<!--android:layout_height="wrap_content" />-->
<!--</LinearLayout>-->
<!--<ImageView-->
<!--android:layout_width="300dp"-->
<!--android:layout_height="300dp"-->
<!--android:id="@+id/iv_main_image"-->
<!--/>-->
<!--<ImageView-->
<!--android:layout_width="100dp"-->
<!--android:id="@+id/iv_main_imageNew"-->
<!--android:layout_height="100dp"-->
<!--/>-->