欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android图片转换器示例

程序员文章站 2023-08-29 19:39:07
mainactivity.java 复制代码 代码如下:package com.zhang.showphoto; import android.app.actionbar...

mainactivity.java

复制代码 代码如下:

package com.zhang.showphoto;

import android.app.actionbar.layoutparams;
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.animation.animationutils;
import android.widget.button;
import android.widget.imageswitcher;
import android.widget.imageview;
import android.widget.viewswitcher.viewfactory;

public class mainactivity extends activity {

 private int[] imagid=new int[]{
   r.drawable.img01,r.drawable.img02,r.drawable.img03,r.drawable.img04,
   r.drawable.img05,r.drawable.img06,r.drawable.img07,r.drawable.img08,
   r.drawable.img09,r.drawable.img10
  };
 private int index=0;
 private imageswitcher imageswitcher;
 private button up,down;
 

 @override
 protected void oncreate(bundle savedinstancestate) {

  super.oncreate(savedinstancestate);
  setcontentview(r.layout.main);

  up=(button) findviewbyid(r.id.bt1);
  down=(button) findviewbyid(r.id.bt2);

 
  imageswitcher=(imageswitcher) findviewbyid(r.id.imagsw1);
  imageswitcher.setinanimation(animationutils.loadanimation(this, android.r.anim.fade_in));
  imageswitcher.setoutanimation(animationutils.loadanimation(this, android.r.anim.fade_out));
  imageswitcher.setfactory(new viewfactory() {

  
   public view makeview() {
    imageview imageview = new imageview(mainactivity.this);
    imageview.setscaletype(imageview.scaletype.fit_center);
    imageview.setlayoutparams(new imageswitcher.layoutparams(
      layoutparams.wrap_content,layoutparams.wrap_content
      ));
    return imageview;
   }
  });

  imageswitcher.setimageresource(imagid[index]);

  up.setonclicklistener(new onclicklistener() {

   @override
   public void onclick(view v) {
    if(index>0){
     index--;
    }else{
     index=imagid.length-1;
    }
    imageswitcher.setimageresource(imagid[index]);
   }
  });

  down.setonclicklistener(new onclicklistener() {

   @override
   public void onclick(view v) {
    if(index<imagid.length-1){
     index++;
    }else{
     index=0;
    }
    imageswitcher.setimageresource(imagid[index]);
   }
  });
 }
}

main.xml

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bg1"
    android:id="@+id/llayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一张"
        android:id="@+id/bt1"
        />
    <imageswitcher
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imagsw1"
        android:layout_gravity="center"
        />
     <button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张"
        android:id="@+id/bt2"
        />

</linearlayout>