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

Android图片转换器代码分享

程序员文章站 2024-03-06 15:47:26
mainactivity.java package com.zhang.showphoto; import android.app.actionbar.l...

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>

再来看一段代码

    // 获取图片的宽高
    bitmapfactory.options opt = new bitmapfactory.options();
    opt.injustdecodebounds = true;
    try{
      bitmapin = bitmapfactory.decodefile(puzzle.user.custom_image[customimage], opt);
    }catch(exception e){
      if(d) log.d(tag,"error");
      return;
    }
    int in_w=opt.outwidth,in_h=opt.outheight;
     
    // 获取imageview的尺寸 注意imageview的宽高比要与原图相同 否则需要另行计算
    full_w = imageview.getwidth()
    full_h = getheight()
 
    // 计算缩放比例 带有四舍五入
    int size_rate=(in_w*in_h*10)/(full_w*full_h);
    if(size_rate>10){
      size_rate+=5; 
      size_rate/=10;
    }else{
      size_rate=1;
    }
 
    // 重新设置opt 读取图片文件
    opt.insamplesize=size_rate;
    opt.injustdecodebounds = false;
    opt.inscaled = false;
 
    opt.outwidth=full_w;
    opt.outheight=full_h;
    bitmapin = bitmapfactory.decodefile(puzzle.user.custom_image[customimage], opt);}