2011-06-22 32 views
0

我與我的代碼,它正在運行成功,但我想添加觸摸事件對它來改變圖像,也爲放大和縮小效果.... 這是我的代碼:圖像切換器上的觸摸事件

package com.conn; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
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.AdapterView.OnItemClickListener; 
import android.widget.ViewSwitcher.ViewFactory; 

public class image_slider extends Activity implements ViewFactory { 

    Integer pics[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, 
      R.drawable.e }; 

    ImageSwitcher iSwitcher; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01); 
     iSwitcher.setFactory(this); 
     iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_in)); 
     iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_out)); 

     Gallery gallery = (Gallery) findViewById(R.id.Gallery01); 
     gallery.setAdapter(new ImageAdapter(this)); 
     gallery.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       iSwitcher.setImageResource(pics[arg2]); 
      } 
     }); 
    } 

    public class ImageAdapter extends BaseAdapter { 

     private Context ctx; 

     public ImageAdapter(Context c) { 
      ctx = c; 
     } 

     @Override 
     public int getCount() { 

      return pics.length; 
     } 

     @Override 
     public Object getItem(int arg0) { 

      return arg0; 
     } 

     @Override 
     public long getItemId(int arg0) { 

      return arg0; 
     } 

     @Override 
     public View getView(int arg0, View arg1, ViewGroup arg2) { 

      ImageView iView = new ImageView(ctx); 
      iView.setImageResource(pics[arg0]); 
      iView.setScaleType(ImageView.ScaleType.FIT_XY); 
      iView.setLayoutParams(new Gallery.LayoutParams(150, 150)); 
      return iView; 
     } 

    } 

    @Override 
    public View makeView() { 
     ImageView iView = new ImageView(this); 
     iView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     iView.setLayoutParams(new 
       ImageSwitcher.LayoutParams(
         LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
     iView.setBackgroundColor(0xFF000000); 
     return iView; 
    } 
} 

回答

0

通過下面的鏈接,它具有您需要的所有功能。

PinchZooming

http://code.google.com/p/android-pinch/wiki/PinchImageView http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/

一指變焦

http://blogs.sonyericsson.com/wp/2010/05/18/android-one-finger-zoom-tutorial-part-1/

希望這有助於。

我已經修改了類按您的需求。這在這裏使用PinchImageView,而不是簡單的ImageView..so

檢查... HTTP://pastebin.com/eRH2D2nF

謝謝.. Giri

+0

感謝您的幫助bt它不工作真的..可以在我的代碼中通過編輯實現這些東西.........? – sunny

+0

檢查我的最新ans.its鏈接修改後的代碼,根據您的鏈接 –

+0

它可能是如此簡單,但對像我這樣的新用戶來說,它有點困難......因爲我還沒有用過觸摸事件。 ...你可以編輯我的代碼並糾正那個....?我很容易理解爲初學者..... – sunny