Android:ImageView控件显示图片

时间:2023-03-09 00:49:16
Android:ImageView控件显示图片

1)android显示图片可以使用imageView来呈现,而且也可以通过ImageButton来实现给button添加图片。

2)在创建一个ImageView后,显示图片绑定元素是:android:src="@android:drawable/btn_dialog"

3)后台操作绑定图片函数:imgView.setImageResource(R.drawable.pic0);,其中drawable是我们可以显示工程res下所有drawable的文件夹中的图片元素的。

学习示例,实现一个前后翻页切换图片的功能:

效果:

Android:ImageView控件显示图片

源代码:res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="74dp"
android:text="" /> <Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="54dp"
android:layout_marginTop="18dp"
android:layout_toRightOf="@+id/btnPrevious"
android:text="下一张" /> <Button
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnNext"
android:layout_alignBottom="@+id/btnNext"
android:layout_marginLeft="21dp"
android:layout_toRightOf="@+id/tvShow"
android:text="上一张" /> <ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnPrevious"
android:layout_alignRight="@+id/btnNext"
android:layout_below="@+id/btnPrevious"
android:layout_marginTop="24dp"
android:src="@android:drawable/btn_dialog" /> </RelativeLayout>

后台源代码:

 package com.example.helloword;

 import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.renderscript.Script.KernelID;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends Activity {
private Button btnNext, btnPrevious;
private ImageView imgView;
private int imgIndex = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnNext = (Button) this.findViewById(R.id.btnNext);
btnPrevious = (Button) this.findViewById(R.id.btnPrevious);
imgView = (ImageView) this.findViewById(R.id.imgView); final int[] imageItems = new int[] { R.drawable.pic0, R.drawable.pic1,
R.drawable.pic2 }; imgView.setImageResource(imageItems[imgIndex]);
btnPrevious.setEnabled(false);
btnNext.setEnabled(true); btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
imgIndex = imgIndex + 1;
if (imgIndex >= imageItems.length - 1) {
btnNext.setEnabled(false);
} else {
btnNext.setEnabled(true);
} if (imgIndex > 0) {
btnPrevious.setEnabled(true);
}else{
btnPrevious.setEnabled(false);
} imgView.setImageResource(imageItems[imgIndex]);
}
}); btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
imgIndex = imgIndex - 1; if (imgIndex == 0) {
btnPrevious.setEnabled(false);
} else {
btnPrevious.setEnabled(true);
} if (imgIndex >= imageItems.length - 1) {
btnNext.setEnabled(false);
} else {
btnNext.setEnabled(true);
} imgView.setImageResource(imageItems[imgIndex]);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// 当点击回退时,弹出该窗口(也就相当于关闭操作)
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this).setTitle("是否退出?")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).setNegativeButton("取消", null).show();
return true;
}
return super.onKeyUp(keyCode, event);
}
}