废话不说,直接上干货:
开发工具:Android Studio 4.1.3
开发语言:kotlin
重要的话写在前面: 要做两张或多张图片轮流交换,必须要得到图片的id号,而这个正在显示的图片的id号是不好直接得到的,
但可以使用getTag()得到。 以下程序就是通过这个“窍门” 弄出的结果 :)
第一步:准备两张图片jpg或png都可以,文件名为ab1.jpg和ab2.jpg
第二步:新建一个新的Android新工程,在编程环境下,将这两张图片拷贝粘贴到drawable资源目录下。
第三步:写布局:
<ImageView
android:id="@+id/i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ab1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="换图片"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/i1"/>
ImageView控件的目的就是为了显示图片,Button的目的就是为了让这两张图片进行轮换
第三步:在kotlin的程序
class MainActivity : AppCompatActivity(),View.OnClickListener{
var btn1:Button?=null
var i1:ImageView?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn1=findViewById<Button>(R.id.btn1)
i1=findViewById<ImageView>(R.id.i1)
btn1?.setOnClickListener(this)
}
override fun onClick(v: View?) {
when(v?.id){
R.id.btn1 -> {
if (i1?.getTag()==R.drawable.ab1){
i1?.setImageResource(R.drawable.ab2)
i1?.setTag(R.drawable.ab2)
}else{
i1?.setImageResource(R.drawable.ab1)
i1?.setTag(R.drawable.ab1)
}
}
}
}
}
运行后,你点击换图片按钮,就可以在两幅图片之间进行交换了。
--------------------------------------------------------------------------------------------
还有一种做法就是用反射,使用反射的方法获取ImageView的id。
AppCompatImageView 这个是V7包的ImageView,可以看到有两个私有属性:有私有属性就可以用反射, 因为公有属性用反射是没有必要的。
private final AppCompatBackgroundHelper mBackgroundTintHelper;
private final AppCompatImageHelper mImageHelper;
再看两个属性对应的类源码,发现AppCompatImageHelper并没有私有属性,所以无法进行反射。
而AppCompatBackgroundHelper是有私有属性的,所以可以反射到mBackgroundResId ,所以这个就是我们要的id
private int mBackgroundResId = -1;
void onSetBackgroundResource(int resId) {
this.mBackgroundResId = resId; //你看这里就可以得到你设置背景的图片id号,这也告诉我们,只要把前景图片设置为背景,就可以得到图片的id号了。
this.setInternalBackgroundTint(this.mDrawableManager != null ? this.mDrawableManager.getTintList(this.mView.getContext(), resId) : null);
this.applySupportBackgroundTint();
}
因此,我们只能通过设置背景的方法获取图片id,不能使用setImageResource
为了获得图片id,我们做一个单例工具类:建一个工具包,在工具包中新建一个Utils单例类,类中再建一个
object Utils { fun getV7ImageResourceId(imageView: ImageView): Int { var imgid = 0 val fields = imageView.javaClass.declaredFields for (f in fields) { if (f.name == "mBackgroundTintHelper") { f.isAccessible = true try { val obj = f[imageView] val fields2 = obj.javaClass.declaredFields for (f2 in fields2) { if (f2.name == "mBackgroundResId") { f2.isAccessible = true imgid = f2.getInt(obj) Log.d("1111", "Image ResourceId:$imgid") } } } catch (e: IllegalAccessException) { e.printStackTrace() } } } return imgid } }
然后结合下面的语句就可以得使用了:
在Activity里设置背景图片
ImageView imageView = findViewById(R.id.commit_img);
imageView.setBackgroundResource(R.drawable.testimg);
int imgid = Utils.getV7ImageResourceId(imageView);
完毕,完整代码如下:
class MainActivity : AppCompatActivity(), View.OnClickListener{ var btn1: Button?=null var i1: ImageView?=null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn1=findViewById<Button>(R.id.btn1) i1=findViewById<ImageView>(R.id.i1) i1?.setBackgroundResource(R.drawable.ab1) btn1?.setOnClickListener(this) } override fun onClick(v: View?) { val imgid: Int = Utils.getV7ImageResourceId(i1!!) when(v?.id){ R.id.btn1 -> { if(imgid==R.drawable.ab1){ i1?.setImageResource(R.drawable.ab2) i1?.setBackgroundResource(R.drawable.ab2) }else{ i1?.setImageResource(R.drawable.ab1) i1?.setBackgroundResource(R.drawable.ab1) } } } } }
点击按钮时,就可以两个图片进行反复交换了。