激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - android ImageView 的幾點經(jīng)驗總結(jié)

android ImageView 的幾點經(jīng)驗總結(jié)

2021-01-31 18:06Android開發(fā)網(wǎng) Android

本篇文章是對android中ImageView的使用技巧進行了幾點經(jīng)驗總結(jié),需要的朋友參考下

最近作圖片的顯示,遇到了些問題,簡單總結(jié)
1)可以用ImageSwicher和ImageView結(jié)合在來做,這樣會用到setFectory(),華而不實
最要命的是如果圖片的大小超過屏幕,實現(xiàn)比較困難,目前是沒有找到方法

2)最簡單的方法是用ImageView,圖片直接FIT_CENTER,android會根據(jù)圖片的大小自動調(diào)節(jié)
保持圖片的比例。如果圖片分辨率超過屏幕,android也會自動的調(diào)整到屏幕能放下整張的圖片
在放大圖片的時候,可以用ImageView的SetFrame() 和setScale()方法,可以把圖片放大
到超過屏幕,原理就是ImageView放大,圖片跟著放大。同時也是可以添加各種animation.
大致如下:

復(fù)制代碼 代碼如下:

Animation animation = AnimationUtils.loadAnimation(Main.this, R.anim.my_scale_action);
imageView.setLayoutParams(new Gallery.LayoutParams(206, 206));
imageView.startAnimation(animation);


寫一個自己的MyImageView類,代碼如下,可以直接用

復(fù)制代碼 代碼如下:


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;
    //創(chuàng)建一個自己的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);
        }
        //用來設(shè)置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; 
            } 
        } 

   //這里來監(jiān)聽屏幕觸控時間 
   @Override 
    public boolean onTouchEvent(MotionEvent event) { 

       /*
        * 判定用戶是否觸摸到了圖片
        * 如果是單點觸摸則調(diào)用控制圖片移動的方法
        * 如果是2點觸控則調(diào)用控制圖片大小的方法
        */ 
        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; 
    }        

}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九一免费在线观看 | 日韩av片网站 | 在线成人一区二区 | 涩涩操| 日韩中文字幕三区 | 一区二区免费看 | 国产一区二区三区视频在线观看 | 欧美一级淫片免费视频1 | 成年免费大片黄在线观看岛国 | 久久精品在这里 | www69xxx | 一级片在线免费 | 久久成人黄色 | 久久久久电影网站 | 日本精品视频一区二区三区四区 | 中文字幕综合在线观看 | 欧美性受xxxx白人性爽 | 国产99视频在线观看 | 国产精品视频自拍 | 日本视频免费 | 可以看逼的视频 | 中文日韩| 国产精品色综合 | 亚洲国产精品久久久久制服红楼梦 | 欧美一级高清片_欧美高清aa | 国产午夜精品久久久久 | 久久亚洲第一 | 免费观看欧美一级片 | 精品一区二区三区电影 | 青草久久网 | 日本一区二区高清不卡 | 欧美一级视频在线 | china对白普通话xxxx | 91成人免费网站 | 久久久久久久久成人 | 黄色片免费在线 | 在线观看精品视频 | 亚洲国产一区二区三区 | 一级黄色片在线看 | 看个毛片| 性欧美久久 |