Android 응용 프로그램 에서 그림 탐색 시 자동 전환 기능 을 구현 하 는 방법 에 대한 자세 한 설명

14760 단어 Android그림 전환
먼저 최종 효과 도 를 드 립 니 다:
201647182619352.gif (326×484)
우리 가 맨 아래 갤러리 에서 그림 을 전환 할 때 위의 큰 그림 은 자동 으로 전환 되 고 전환 할 때 애니메이션 효과 가 있 습 니 다.간단 한 프로그램 입 니 다.더 많은 기능 을 보완 해 야 합 니 다!
activity 코드:

package cn.com.chenzheng_java; 
 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.animation.AnimationUtils; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ImageView.ScaleType; 
import android.widget.ViewSwitcher.ViewFactory; 

public class GalleryActivity extends Activity implements OnItemSelectedListener { 
 
  int[] imagesId = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3, 
      R.drawable.a4 
 
  }; 
  int index = R.drawable.a1; 
  ImageSwitcher imageSwitcher; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    /** 
     *         ,    setContentView       
     */ 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.gallery); 
 
    Gallery gallery = (Gallery) findViewById(R.id.gallery1); 
    gallery.setOnItemSelectedListener(this); 
 
    imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); 
    imageSwitcher.setVisibility(View.VISIBLE); 
     
    /*** 
     * setInAnimation         
     * setOutAnimation         
     */ 
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation( 
        getApplicationContext(), android.R.anim.fade_in)); 
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation( 
        getApplicationContext(), android.R.anim.fade_out)); 
 
    imageSwitcher.setFactory(new ViewFactory() { 
      /** 
       *            ImageSwitcher ,          。 
       *             。                       。 
       *         setView   
       */ 
      @Override 
      public View makeView() { 
        ImageView imageView = new ImageView(GalleryActivity.this); 
        //        
        imageView.setScaleType(ScaleType.CENTER_INSIDE); 
        imageView.setBackgroundResource(R.drawable.ground); 
        Toast.makeText(getApplicationContext(), "     ", 
            Toast.LENGTH_SHORT).show(); 
        return imageView; 
      } 
    }); 
     
    gallery.setVisibility(View.VISIBLE); 
    gallery.setAdapter(new ImageAdapter(this)); 
 
  } 
 
  class ImageAdapter extends BaseAdapter { 
    private Context context2; 
 
    public ImageAdapter(Context context) { 
      context2 = context; 
    } 
 
    @Override 
    public int getCount() { 
      return imagesId.length; 
    } 
 
    @Override 
    public Object getItem(int position) { 
      return imagesId[position]; 
    } 
 
    @Override 
    public long getItemId(int position) { 
      return position; 
    } 
 
    @Override 
    public View getView(int position, View image2, ViewGroup parent) { 
      ImageView image = new ImageView(context2); 
      image.setBackgroundResource(imagesId[position]); 
      /** 
       * setScaleType()                       . 
       * ScaleType.CENTER_INSIDE    ,             ,                   
       */ 
      image.setScaleType(ScaleType.CENTER_INSIDE); 
 
      return image; 
    } 
 
  } 
  /** 
   *  gallery        ,  ImageSwitcher    
   */ 
  @Override 
  public void onItemSelected(AdapterView<?> parent, View view, int position, 
      long id) { 
    imageSwitcher.setImageResource(imagesId[position]); 
 
  } 
 
  @Override 
  public void onNothingSelected(AdapterView<?> parent) { 
 
  } 
 
} 

gallery.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" 
 android:gravity="center_horizontal" 
 > 
  
 <ImageSwitcher 
  android:id="@+id/imageSwitcher" 
  android:layout_height="350dp" 
  android:layout_width="fill_parent"  
 ></ImageSwitcher> 
   
  <Gallery android:id="@+id/gallery1"  
  android:gravity="center" 
  android:layout_width="70dp"  
  android:layout_height="100dp"></Gallery> 
</LinearLayout> 

업그레이드 버 전
위의 것 에 비해 효 과 는 똑 같 습 니 다.다만 여기에 다음 과 같은 몇 가지 기능 을 추 가 했 습 니 다.
첫째:아래 그림 의 자가 적응 길이(길이 와 너비 비례 를 보장 하 는 경우)
둘째:큰 그림 을 만 져 도 그림 전환 을 할 수 있다(기계 가 없 으 면 테스트 할 수 없 지만 기본 적 인 사고방식 은 정확 하 다)
코드:
activity 코드:

package cn.com.chenzheng_java; 
 
import java.text.DecimalFormat; 
 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.View.OnTouchListener; 
import android.view.animation.AnimationUtils; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.Gallery.LayoutParams; 
import android.widget.ImageView.ScaleType; 
import android.widget.ViewSwitcher.ViewFactory; 

public class GalleryActivity extends Activity implements OnItemSelectedListener ,OnTouchListener{ 
 
  int[] imagesId = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3, 
      R.drawable.a4 
 
  }; 
  int index = 0; 
  ImageSwitcher imageSwitcher; 
  int maxImageWidth = 70; 
  int maxImageHeight = 100; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    /** 
     *         ,    setContentView       
     */ 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.gallery); 
 
    Gallery gallery = (Gallery) findViewById(R.id.gallery1); 
    gallery.setOnItemSelectedListener(this); 
 
    imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); 
    imageSwitcher.setVisibility(View.VISIBLE); 
     
    /*** 
     * setInAnimation         
     * setOutAnimation         
     */ 
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation( 
        getApplicationContext(), android.R.anim.fade_in)); 
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation( 
        getApplicationContext(), android.R.anim.fade_out)); 
 
    imageSwitcher.setFactory(new ViewFactory() { 
      /** 
       *            ImageSwitcher ,          。 
       *             。                       。 
       *         setView   
       */ 
      @Override 
      public View makeView() { 
        ImageView imageView = new ImageView(GalleryActivity.this); 
        //        
        imageView.setScaleType(ScaleType.CENTER_INSIDE); 
        imageView.setBackgroundResource(R.drawable.ground); 
        Toast.makeText(getApplicationContext(), "     ", 
            Toast.LENGTH_SHORT).show(); 
        return imageView; 
      } 
    }); 
     
    gallery.setVisibility(View.VISIBLE); 
    gallery.setAdapter(new ImageAdapter(this)); 
 
  } 
 
  class ImageAdapter extends BaseAdapter { 
    private Context context2; 
 
    public ImageAdapter(Context context) { 
      context2 = context; 
    } 
 
    @Override 
    public int getCount() { 
      return imagesId.length; 
    } 
 
    @Override 
    public Object getItem(int position) { 
      return imagesId[position]; 
    } 
 
    @Override 
    public long getItemId(int position) { 
      return position; 
    } 
    /*** 
     *             ,      ,             ,                 
     *        BitmapFactory Bitmap           
     * DecaimalFormat    double        (            ) 
     */ 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      index = position; 
      ImageView image = new ImageView(context2); 
      Resources resources = context2.getResources(); 
      //      Bitmap   
      Bitmap bitmap = BitmapFactory.decodeResource(resources, imagesId[position]); 
      Log.i("    decode    ", (bitmap!=null)+""); 
       
      //         
      int bitMapWidth = bitmap.getWidth(); 
      int bitMapHeight = bitmap.getHeight(); 
      Log.i("bitMapWidth", bitMapWidth+""); 
      Log.i("bitMapHeight", bitMapHeight+""); 
       
      //             
      double scale; 
      double widthScale = (maxImageWidth*100)/(bitMapWidth*100.0);//    100    100.0          double   
      double heightScale = (maxImageHeight*100)/(bitMapHeight*100.0); 
       
      //        (      ) 
      DecimalFormat decimalFormat = new DecimalFormat("#.00"); 
      widthScale = Double.valueOf(decimalFormat.format(widthScale)); 
      heightScale = Double.valueOf(decimalFormat.format(heightScale)); 
       
      Log.i("widthScale", widthScale+""); 
      Log.i("heightScale", heightScale+""); 
       
       
      if(widthScale>= heightScale){ 
        scale = widthScale; 
      } 
      else{ 
        scale = heightScale; 
      } 
       
      Log.i("scale", scale+""); 
       
      //                 
      Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, (int)(bitMapWidth*scale), (int)(bitMapHeight*scale), true); 
      //       ImageView 
      image.setImageBitmap(bitmap2); 
       
      /** 
       * setScaleType()                       . 
       * ScaleType.CENTER_INSIDE    ,             ,                   
       *                     gallery ,         ,            
       * setScaleType     。             ,      ,        ,   ,     
       *           ,    bitmap          ,     、    …… 
       */ 
      // image.setScaleType(ScaleType.CENTER_INSIDE); 
      image.setAdjustViewBounds(true);//         
      image.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      return image; 
    } 
 
  } 
  /** 
   *  gallery        ,  ImageSwitcher    
   */ 
  @Override 
  public void onItemSelected(AdapterView<?> parent, View view, int position, 
      long id) { 
    imageSwitcher.setImageResource(imagesId[position]); 
 
  } 
 
  @Override 
  public void onNothingSelected(AdapterView<?> parent) { 
 
  } 
  int i = 0 ; 
  /*** 
   *            ,               ,        o 
   */ 
  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
    Log.i("      ", "yes"); 
    int actionName = event.getAction(); 
    float []position = null; 
     
    //     
    if(actionName==MotionEvent.ACTION_DOWN){ 
      i = 0; 
    } 
    //        
    if(actionName == MotionEvent.ACTION_MOVE) 
    { 
      position[i] = event.getX(); 
      ++i; 
       
    } 
    if(actionName == MotionEvent.ACTION_UP) 
    { 
      if(position[position.length]>position[position.length-1]){ 
         
        if((index+1)>imagesId.length){ 
          imageSwitcher.setImageResource(imagesId[index]); 
        } 
        else{ 
          imageSwitcher.setImageResource(imagesId[index+1]); 
        } 
         
         
      } 
      else{ 
        if((index-1)<0){ 
          imageSwitcher.setImageResource(imagesId[0]); 
        } 
        else{ 
          imageSwitcher.setImageResource(imagesId[index-1]); 
        } 
         
      } 
    } 
    return true; 
  } 
 
} 

gallery.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" 
 android:gravity="center_horizontal" 
 > 
  
 <ImageSwitcher 
  android:id="@+id/imageSwitcher" 
  android:layout_height="350dp" 
  android:layout_width="fill_parent"  
 ></ImageSwitcher> 
   
  <Gallery android:id="@+id/gallery1"  
  android:gravity="center" 
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"></Gallery> 
</LinearLayout> 

좋은 웹페이지 즐겨찾기