2011-11-24 75 views
0

你好傢伙我是新來的機器人,所以要溫柔與我:),
我有畫廊我需要加載圖像我有很多的他們,圖像加載性能我已經通過使用AsyncTask解決了。
Android:圖片與滑動跳過圖像和緩慢加載

現在我有滾動畫廊的問題,它跳過圖像,它看起來不好。

現在我試圖實現我的自定義庫,並在沒有成功,它只是將無法正常工作
現在我還有一個問題

final ScrollableGallery galleryView = (ScrollableGallery)findViewById(R.id.gallery); //always returns null 

突然,你能explane爲什麼嗎?

下面的代碼:

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.Gallery; 

public class ScrollableGallery extends Gallery { 

    public ScrollableGallery(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public ScrollableGallery(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ScrollableGallery(Context context) { 
     super(context); 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

     // limit the max speed in either direction 
     if (velocityX > 1200.0f) { 
      velocityX = 1200.0f; 
     } else if (velocityX < 1200.0f) { 
      velocityX = -1200.0f; 
     } 
     return super.onFling(e1, e2, velocityX, velocityY); 
    } 
} 

的XML部分:

<ScrollableGallery 
     android:id="@+id/gallery" 
     android:layout_width="675dp" 
     android:layout_height="492dp" 
     android:layout_marginLeft="62dp" 
     android:layout_marginTop="225dp" 
     android:scaleType="fitXY" > 
    </ScrollableGallery> 


的Actvity調用:
的啓動

final ScrollableGallery galleryView = (ScrollableGallery)findViewById(R.id.gallery); 


適配器開始:

galleryV.setAdapter(new BaseAdapter() {      
      public int getCount() {    
       return _imageList.size(); 
      } 

      public Object getItem(int position) { 
       return _imageList.get(position); 
      } 

      public long getItemId(int position) { 
       _position = position; 
       return position; 
      } 

      public View getView(final int position, View convertView,final ViewGroup parent) {    
       if (convertView == null) { 
        convertView = new ImageView(Database.this); 
       }    
       ImageDispatcher dispatch = new ImageDispatcher(((ImageView) convertView));    
       dispatch.execute(_imageList.get(position));          
       ((ImageView) convertView).setScaleType(ImageView.ScaleType.FIT_XY); 
       ((ImageView) convertView).setLayoutParams(new ScrollableGallery.LayoutParams(675, 492)); 

       return convertView; 
      }   
     }); 
<br/> 
The image dispatcher: 

    import java.lang.ref.WeakReference; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.os.AsyncTask; 
    import android.widget.ImageView; 

    public class ImageDispatcher extends AsyncTask<String, Void, Bitmap> { 

     private final WeakReference<ImageView> imageViewReference; 

     public ImageDispatcher(ImageView imageView) { 
      imageViewReference = new WeakReference<ImageView>(imageView); 
     } 

     @Override 
     // Actual download method, run in the task thread 
     protected Bitmap doInBackground(String... params) { 

      return ShrinkBitmap(params[0], 300 ,300); 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      if (isCancelled()) { 
       bitmap = null; 
      } 

      if (imageViewReference != null) { 
       ImageView imageView = imageViewReference.get(); 
       if (imageView != null) { 
        imageView.setImageBitmap(bitmap); 
       } 
      } 
     } 

     /********************************************************* 
     * PRIVATE METHODS 
     *********************************************************/ 
     @SuppressWarnings("unused") 
     private Bitmap ShrinkBitmap(String file, int width, int height) { 
      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
      bmpFactoryOptions.inTempStorage = new byte[16 * 1024]; 
      bmpFactoryOptions.inJustDecodeBounds = true; 
      Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
      int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float) height); 
      int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float) width); 

      bmpFactoryOptions.inSampleSize = heightRatio > 1 || widthRatio > 1 ? heightRatio : widthRatio;     
      bmpFactoryOptions.inJustDecodeBounds = false; 
      bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
      return bitmap; 
     } 

     @SuppressWarnings("unused") 
     private Bitmap ShrinkBitmap(String file) { 
      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
      bmpFactoryOptions.inTempStorage = new byte[16 * 1024]; 
      bmpFactoryOptions.inJustDecodeBounds = true; 

      Bitmap bMap = BitmapFactory.decodeFile(file);  
      Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap,150, 100, true); 

      return bMapScaled; 
     } 
    } 

我覺得這一切都讓我對這個部分的代碼, 現在的問題是:
如何使滾動中,一個圖像時間,以及如何顯示加載指標,如我在網絡中加載圖像時?

任何代碼修復將高度讚賞!

預先感謝您...

回答

0

而不是聲明你在xml中的畫廊。

Gallery gallery = new CustomGaller(getActivity()); 

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
      params.addRule(RelativeLayout.RIGHT_OF, R.id.left); 
      params.addRule(RelativeLayout.LEFT_OF,R.id.right); 
      gallery.setLayoutParams(params); 
      gallery.setAdapter(new GalleryAdapter(<Pass values>)); 

您不得不將View id傳遞給CustomGallery。

0

在您的自定義庫

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { 
    -->> super.onFling(e1, e2, 20, velocityY); <<-- 

    return false; 
} 

嘗試修改的第三個參數。

+0

沒有工作 – IamStalker