首页 > 代码库 > 17 UI组件之 Basic View- ImageView
17 UI组件之 Basic View- ImageView
UI组件之 Basic View- ImageView-图片浏览器练习
ImageView:
小案例1(点击可以切换图片——有序图片浏览):
代码:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a1"/> </LinearLayout>
Test_imageViewActivity.java:
package test.imageView; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class Test_imageViewActivity extends Activity { //该数组为4张图片进行了简单的排序 private int[] a = new int[]{ R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4 }; //设定简单计数器, private int count=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageView = (ImageView)findViewById(R.id.imageView1); imageView.setOnClickListener(new OnClickListener(){ public void onClick(View v) { ImageView view =(ImageView) v; if(count==3){ count=0; } view.setImageResource(a[++count]); } }); } }
运行结果:
小案例2(点击可以切换图片——点击图片左边正序浏览图片,点击图片右边逆序浏览图片):
代码:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a1"/> </LinearLayout>
Test_imageViewActivity.java:
package test.imageView; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.ImageView; public class Test_imageViewActivity extends Activity { //该数组为4张图片进行了简单的排序 private int[] a = new int[]{ R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4 }; //设定简单计数器, private int count=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageView = (ImageView)findViewById(R.id.imageView1); //如何获取到屏幕的宽度 final int width = this.getWindowManager().getDefaultDisplay().getWidth(); imageView.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { //如何在图片点击的过程中,获得点击x轴的坐标 int x =(int) event.getX(); ImageView view =(ImageView) v; if(x>=(width/2)){ //右侧 if(count==3){ count=-1;//当++count之后就变为0了 } view.setImageResource(a[++count]); }else{ //左侧 if(count==0){ count=4;//当--count之后就变为3了 } view.setImageResource(a[--count]); } return false; } }); } }
运行结果:
当点击图片的左边进行逆序浏览,点击图片右边进行正序浏览;
17 UI组件之 Basic View- ImageView
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。