Android:带有滑动的图库会跳过图像并且加载缓慢
大家好,我是 Android 新手,所以请对我温柔一些:),
我有一个画廊,我需要加载图像,我有很多图像,我已经通过使用 AsyncTask 解决了图像加载的性能。
现在我遇到了滚动画廊问题,它会跳过图像,而且看起来不太好。
现在我尝试实现我的自定义图库但没有成功,它只是行不通
现在我突然又有一个问题
final ScrollableGallery galleryView = (ScrollableGallery)findViewById(R.id.gallery); //always returns null
,你能解释一下为什么吗?
这是代码:
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>
活动调用:
启动
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;
}
}
我认为这就是我这部分的所有代码, 现在的问题是:
我如何制作滚动条,及时显示一张图像,以及如何在加载图像时像在网络中那样显示加载指示器???
任何代码修复将不胜感激!
先感谢您...
Hello guys i am new to android so be gentle with me :),
I have gallery i need to load images i have lot's of them, the performance on image loading i have solved by using AsyncTask.
Now i have the scrolling on gallery problem, it's skips images and it's not looking nice.
Now i have tried to implement my custom Gallery and no success on that, it's just won't work
now i have one more question the
final ScrollableGallery galleryView = (ScrollableGallery)findViewById(R.id.gallery); //always returns null
suddenly , can you explane why please?
Here's the code:
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);
}
}
The XML part:
<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>
The Actvity Invocation:
The initiation
final ScrollableGallery galleryView = (ScrollableGallery)findViewById(R.id.gallery);
The adapter initiation:
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;
}
}
I think it's all the code that i have for this part,
now is the question:
How do i make the scroll, one image in time, and how do i show loading indicator like i do in web when i load images ???
Any code fixing would be highly appreciated !
Thank you in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是在 xml 中声明 Gallery。
您不必将 View id 传递给 CustomGallery。
Instead of declaring Gallery in you xml.
You dant have to pass View id to CustomGallery.
在您的自定义图库中
尝试修改第三个参数。
In your custom Gallery
Try modifying the 3rd parameter.