Home>

The imageview control is a picture control.Responsible for displaying pictures.

The following simulated phone picture viewer

Directory Structure:

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <imageview android:id="@ + id/imageview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:src="@ drawable/p1" />
 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal">
  <button android:id="@ + id/previous"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="previous"
   android:layout_gravity="center_horizontal" />
  <button android:id="@ + id/alpha_plus"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Transparency increased"
   android:layout_gravity="center_horizontal" />
  <button android:id="@ + id/alpha_minus"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Transparency reduction"
   android:layout_gravity="center_horizontal" />
  <button android:id="@ + id/next"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Next"
   android:layout_gravity="center_horizontal" />
 </linearlayout>
</linearlayout>

imageviewactivity class:

package com.ljq.iv;
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.imageview;
public class imageviewactivity extends activity {
 private imageview imageview=null;
 private button previous=null;//Previous
 private button next=null;//Next
 private button alpha_plus=null;//Transparency increases
 private button alpha_minus=null;//Transparency decreases
 private int currentimgid=0;//Record the id of the image displayed by the current imageview
 private int alpha=255;//Record the transparency of the imageview
 int [] imgid={//imageview display image array
   r.drawable.p1,   r.drawable.p2,   r.drawable.p3,   r.drawable.p4,   r.drawable.p5,   r.drawable.p6,   r.drawable.p7,   r.drawable.p8,  };
 @override
 public void oncreate (bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  setcontentview (r.layout.main);
  imageview=(imageview) findviewbyid (r.id.imageview);
  previous=(button) findviewbyid (r.id.previous);
  next=(button) findviewbyid (r.id.next);
  alpha_plus=(button) findviewbyid (r.id.alpha_plus);
  alpha_minus=(button) findviewbyid (r.id.alpha_minus);
  previous.setonclicklistener (listener);
  next.setonclicklistener (listener);
  alpha_plus.setonclicklistener (listener);
  alpha_minus.setonclicklistener (listener);
 }
 private view.onclicklistener listener=new view.onclicklistener () {
  public void onclick (view v) {
   if (v == previous) {
    currentimgid=(currentimgid-1 + imgid.length)%imgid.length;
    imageview.setimageresource (imgid [currentimgid]);
   }
   if (v == next) {
    currentimgid=(currentimgid + 1)%imgid.length;
    imageview.setimageresource (imgid [currentimgid]);
   }
   if (v == alpha_plus) {
    alpha +=10;
    if (alpha>255) {
     alpha=255;
    }
    imageview.setalpha (alpha);
   }
   if (v == alpha_minus) {
    alpha-=10;
    if (alpha<0) {
     alpha=0;
    }
    imageview.setalpha (alpha);
   }
  }
 };
}

operation result:

  • Previous JavaScript study notes (3): JavaScript also has an entry Main function
  • Next C # using Mutex to easily implement a single instance of the program