android实现简单的图片切换
具体的界面如图
方法一
利用内部类实现
只展示xml文件及MainActivity文件
xml具体代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.ttt.image.MainActivity">
<TextView
android:id="@+id/textShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="0"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textShow"
android:layout_marginBottom="58dp"
android:layout_marginEnd="32dp"
android:layout_toStartOf="@+id/textShow"
android:text="下一张" />
<Button
android:id="@+id/btnPre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnNext"
android:layout_marginStart="23dp"
android:layout_toEndOf="@+id/textShow"
android:text="上一张" />
<ImageView
android:id="@+id/imgshow"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:scaleType="fitXY"
android:src="@drawable/image1" />
</RelativeLayout>
MainActivity文件
package com.example.ttt.image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ImageView imgShow;//定义图像显示框
private Button btnNext,btnPre;//定义按钮
private TextView textShow;//定义文本框
private int[] picArrary;//定义设置数组记录下标
private int index=0;//定义下标值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgShow=findViewById(R.id.imgshow);//获取值
btnNext=findViewById(R.id.btnNext);
btnPre=findViewById(R.id.btnPre);
textShow=findViewById(R.id.textShow);
picArrary=new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6};
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index++;
imgShow.setImageResource(picArrary[index%6]);
textShow.setText(index%6+"");
}
});//实现下一张
btnPre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (index>0){
index--;
imgShow.setImageResource(picArrary[index%6]);
textShow.setText(index%6+"");
}
if (index==0){
index=picArrary.length;
}
}
});//实现上一张
}
}
方法二
利用接口实现
只展示xml文件及MainActivity文件
xml具体代码跟上面一样:
MainActivity文件
package com.example.ttt.image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imgShow;
private Button btnNext,btnPre;
private TextView textShow;
private int[] picArrary;
private int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgShow=findViewById(R.id.imgshow);
btnNext=findViewById(R.id.btnNext);
btnPre=findViewById(R.id.btnPre);
textShow=findViewById(R.id.textShow);
picArrary=new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6};
btnNext.setOnClickListener(this);//利用当前类需添加
btnPre.setOnClickListener(this);//利用当前类需添加
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnNext:
index++;
imgShow.setImageResource(picArrary[index%6]);
textShow.setText(index%6+"");
break;
case R.id.btnPre:
if (index>0){
index--;
imgShow.setImageResource(picArrary[index%6]);
textShow.setText(index%6+"");
}
if (index==0){
index=picArrary.length;
}
}
}
}