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

android ImageView 的几点经验总结

程序员文章站 2024-02-01 23:41:34
最近作图片的显示,遇到了些问题,简单总结1)可以用imageswicher和imageview结合在来做,这样会用到setfectory(),华而不实最要命的是如果图片的大...

最近作图片的显示,遇到了些问题,简单总结
1)可以用imageswicher和imageview结合在来做,这样会用到setfectory(),华而不实
最要命的是如果图片的大小超过屏幕,实现比较困难,目前是没有找到方法

2)最简单的方法是用imageview,图片直接fit_center,android会根据图片的大小自动调节
保持图片的比例。如果图片分辨率超过屏幕,android也会自动的调整到屏幕能放下整张的图片
在放大图片的时候,可以用imageview的setframe() 和setscale()方法,可以把图片放大
到超过屏幕,原理就是imageview放大,图片跟着放大。同时也是可以添加各种animation.
大致如下:

复制代码 代码如下:

animation animation = animationutils.loadanimation(main.this, r.anim.my_scale_action);
imageview.setlayoutparams(new gallery.layoutparams(206, 206));
imageview.startanimation(animation);

写一个自己的myimageview类,代码如下,可以直接用
复制代码 代码如下:

package com.practice.imageviewpic;

import android.app.activity; 
import android.content.context; 
import android.graphics.*; 
import android.graphics.drawable.bitmapdrawable; 
import android.os.bundle; 
import android.util.attributeset;
import android.util.log;
import android.view.motionevent; 
import android.view.view;
import android.view.viewgroup.layoutparams;
import android.widget.button;
import android.widget.imageview; 
import android.widget.imageview.scaletype; 
import android.widget.linearlayout;
    //创建一个自己的imageview类 
    class myimageview extends imageview { 
        private float scale = 0.1f; 

      //两点触屏后之间的长度 
        private float beforelenght; 
        private float afterlenght; 

        //单点移动的前后坐标值 
        private float afterx,aftery; 
        private float beforex,beforey; 

        public myimageview(context context) { 
            super(context); 
        } 
        public myimageview(context context, attributeset attrs) { 
        this(context, attrs, 0);

        }
        public myimageview(context context, attributeset attrs, int defstyle) {
            super(context, attrs, defstyle);
        }
        //用来设置imageview的位置 
        private void setlocation(int x,int y) { 
            this.setframe(this.getleft()+x, this.gettop()+y, this.getright()+x, this.getbottom()+y); 
        } 

         
        /*
         * 用来放大缩小imageview
         * 因为图片是填充imageview的,所以也就有放大缩小图片的效果
         * flag为0是放大图片,为1是小于图片
         */ 
        public void setscale(float temp,int flag) { 

            if(flag==0) { 
                this.setframe(this.getleft()-(int)(temp*this.getwidth()),  
                              this.gettop()-(int)(temp*this.getheight()),  
                              this.getright()+(int)(temp*this.getwidth()),  
                              this.getbottom()+(int)(temp*this.getheight()));    
            }else { 
                this.setframe(this.getleft()+(int)(temp*this.getwidth()),  
                              this.gettop()+(int)(temp*this.getheight()),  
                              this.getright()-(int)(temp*this.getwidth()),  
                              this.getbottom()-(int)(temp*this.getheight())); 
            } 
        } 

        //绘制边框       
         @override 
          protected void ondraw(canvas canvas) { 
              super.ondraw(canvas);     
              rect rec=canvas.getclipbounds(); 
              rec.left++;
              rec.top++;
              rec.bottom--; 
              rec.right--; 
              paint paint=new paint(); 
              paint.setcolor(color.red); 
              paint.setstyle(paint.style.stroke); 
              canvas.drawrect(rec, paint); 
          } 

          
        /* 让图片跟随手指触屏的位置移动
         * beforex、y是用来保存前一位置的坐标
         * afterx、y是用来保存当前位置的坐标
         * 它们的差值就是imageview各坐标的增加或减少值
         */ 
        public void movewithfinger(motionevent event) { 

            switch(event.getaction()) { 

            case motionevent.action_down: 
            //log.d(tag, "down ..");
                beforex = event.getx(); 
                beforey = event.gety(); 
                break; 
            case motionevent.action_move: 

            //log.d(tag, "move ..");
                afterx = event.getx(); 
                aftery = event.gety(); 

                this.setlocation((int)(afterx-beforex),(int)(aftery-beforey)); 

                beforex = afterx; 
                beforey = aftery; 
                break; 

            case motionevent.action_up: 
            //log.d(tag, "up ..");
                break; 
            } 
        } 

        /*
         * 通过多点触屏放大或缩小图像
         * beforelenght用来保存前一时间两点之间的距离
         * afterlenght用来保存当前时间两点之间的距离
         */ 
        public void scalewithfinger(motionevent event) { 
            float movex = event.getx(1) - event.getx(0); 
            float movey = event.gety(1) - event.gety(0); 

            switch(event.getaction()) { 
            case motionevent.action_down: 
                beforelenght = (float) math.sqrt( (movex*movex) + (movey*movey) ); 
                break; 
            case motionevent.action_move: 
                //得到两个点之间的长度 
                afterlenght = (float) math.sqrt( (movex*movex) + (movey*movey) ); 

                float gaplenght = afterlenght - beforelenght; 

                if(gaplenght == 0) { 
                    break; 
                } 

                //如果当前时间两点距离大于前一时间两点距离,则传0,否则传1 
                if(gaplenght>0) { 
                    this.setscale(scale,0); 
                }else { 
                    this.setscale(scale,1); 
                } 

                beforelenght = afterlenght; 
                break; 
            } 
        } 

   //这里来监听屏幕触控时间 
   @override 
    public boolean ontouchevent(motionevent event) { 

       /*
        * 判定用户是否触摸到了图片
        * 如果是单点触摸则调用控制图片移动的方法
        * 如果是2点触控则调用控制图片大小的方法
        */ 
        if(event.gety() > this.gettop() && event.gety() < this.getbottom() 
                && event.getx() > this.getleft() && event.getx() < this.getright()) { 
            if(event.getpointercount() == 2) { 
            this.scalewithfinger(event); 
            }else if(event.getpointercount() == 1) { 
            this.movewithfinger(event); 
            }            
        } 
        return true; 
    }        

}