本篇文章為大家展示了怎么在Android中實現一個圖片點擊切換功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
java 代碼
public class MainActivity extends AppCompatActivity {
//定義一個訪問圖片的數組
int[] images = new int[]{
R.drawable.java,
R.drawable.javaee,
R.drawable.swift,
R.drawable.ajax,
R.drawable.html,
};
//用于圖片切換
int currenImg = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取Linearlayout布局容器
LinearLayout main = (LinearLayout) findViewById(R.id.root);
//創建ImageView組件
final ImageView image = new ImageView(this);
//將ImageView組建添加到linearlayout布局中
main.addView(image);
//初始化顯示第一張圖片
image.setImageResource(images[0]);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(images[++currenImg % images.length]);
}
});
}
}xml 文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> </LinearLayout>
上述內容就是怎么在Android中實現一個圖片點擊切換功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。