在Android中制作全屏图像滑块的最佳方法(如Android Gallery)
我的数据库中有一些(例如:100到200)图像,我必须在应用程序中使用滑块动画显示这些图像,如下所示: 我尝试使用viewpager,但如果我的数据库中有300个图像,我将不得不为viewpager创建300个位图和片段。它应该看起来像android设备在gallery中显示图像的方式(参见上图以供参考)。实现这一目标的最佳方式是什么?我还尝试了自定义的圆形viewpager(它不支持少于4幅的图像) (见附件) 我也尝试了来自的链接,但我不认为这些链接将是一个有效的方式来完成我的任务 (如3页逻辑) 我还有另一个疑问在Android中制作全屏图像滑块的最佳方法(如Android Gallery),android,performance,android-fragments,android-viewpager,Android,Performance,Android Fragments,Android Viewpager,我的数据库中有一些(例如:100到200)图像,我必须在应用程序中使用滑块动画显示这些图像,如下所示: 我尝试使用viewpager,但如果我的数据库中有300个图像,我将不得不为viewpager创建300个位图和片段。它应该看起来像android设备在gallery中显示图像的方式(参见上图以供参考)。实现这一目标的最佳方式是什么?我还尝试了自定义的圆形viewpager(它不支持少于4幅的图像) (见附件) 我也尝试了来自的链接,但我不认为这些链接将是一个有效的方式来完成我的任务 (如3
imageview.setImageURI(Uri.parse(currentvalue.getimagepath()));
如果我使用这段代码将image from db设置为imageview,我可以放弃使用位图吗?或者这种方法也使用位图吗?我认为最有效的方法是使用异步任务。请看一个个人例子:
public class LoadImage extends AsyncTask<Void, Void, String> {
private LoaderImageView loaderImageView;
private int position;
private ImageView image;
private Bitmap bmd;
public LoadImage(LoaderImageView loaderImageView, int position) {
this.loaderImageView = loaderImageView;
this.position = position;
image = loaderImageView.getImageView();
}
@Override
protected void onPreExecute() {
loaderImageView.showProgress();
super.onPreExecute();
}
private void setImageView(int heigthToSet, int weightToSet, int color) {
// set the imageview to the current bitmap
Bitmap bitmap = null;
ReceiptImageDataSource ds = ReceiptImageDataSource.getInstance();
List<ReceiptImage> images = ds.selectReceiptImagesByReceiptID(currentReceipt.getId());
if (images.size() > 0) {
if (BitmapFactory.decodeFile(images.get(position).getPhotoURL()) != null) {
if (images.size() > position) {
bitmap = BitmapFactory.decodeFile(images.get(position).getPhotoURL());
image.setTag(position);
} else {
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo_take_photo);
}
} else {
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo_receipt_x2);
}
}
assert bitmap != null;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// calculate the scale - in this case = 0.6f
float scaleWidth = ((float) weightToSet) / width;
float scaleHeight = ((float) heigthToSet) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
bmd = Utils.getRoundedCornerBitmap(resizedBitmap, color, 8, 0, EditReceiptActivity.this);
}
@Override
protected String doInBackground(Void... arg0) {
int Measuredwidth;
int Measuredheight;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}
setImageView(Measuredheight - Utils.dpToPx(120, EditReceiptActivity.this), Measuredwidth - Utils.dpToPx(60, EditReceiptActivity.this),
Color.TRANSPARENT);
return null;
}
@Override
protected void onPostExecute(String result) {
loaderImageView.hideProgress();
image.setImageBitmap(bmd);
image.setBackgroundColor(Color.TRANSPARENT);
// center the Image
image.setScaleType(ScaleType.FIT_CENTER);
super.onPostExecute(result);
}
}
public类LoadImage扩展异步任务{
私有LoaderImage视图LoaderImage视图;
私人职位;
私有图像查看图像;
私人位图bmd;
公共LoadImage(LoaderImage视图LoaderImage视图,整数位置){
this.loaderImageView=loaderImageView;
这个位置=位置;
image=loaderImageView.getImageView();
}
@凌驾
受保护的void onPreExecute(){
loaderImageView.showProgress();
super.onPreExecute();
}
私有void setImageView(int heightoset、int weightToSet、int color){
//将imageview设置为当前位图
位图=空;
ReceiptImageDataSource ds=ReceiptImageDataSource.getInstance();
List images=ds.selectReceiptImagesByReceiptID(currentReceipt.getId());
如果(images.size()>0){
if(BitmapFactory.decodeFile(images.get(position.getPhotoURL())!=null){
if(images.size()>位置){
bitmap=BitmapFactory.decodeFile(images.get(position.getPhotoURL());
image.setTag(位置);
}否则{
位图=BitmapFactory.decodeResource(getResources(),R.drawable.photo\u take\u photo);
}
}否则{
位图=BitmapFactory.decodeResource(getResources(),R.drawable.no_photo_receive_x2);
}
}
断言位图!=null;
int width=bitmap.getWidth();
int height=bitmap.getHeight();
//计算刻度-在这种情况下=0.6f
浮动比例宽度=((浮动)权重设置)/宽度;
浮动刻度高度=((浮动)高度设置)/高度;
//为操纵创建一个矩阵
矩阵=新矩阵();
//调整位图的大小
矩阵。后标度(标度宽度、标度高度);
//重新创建新位图
Bitmap resizedBitmap=Bitmap.createBitmap(位图,0,0,宽度,高度,矩阵,真);
bmd=Utils.getRoundedCornerBitmap(resizedBitmap,color,8,0,EditReceiptActivity.this);
}
@凌驾
受保护的字符串doInBackground(无效…arg0){
int测量宽度;
内测高度;
点大小=新点();
WindowManager w=getWindowManager();
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.HONEYCOMB){
w、 getDefaultDisplay().getSize(大小);
测量宽度=尺寸x;
测量高度=尺寸y;
}否则{
Display d=w.getDefaultDisplay();
Measuredwidth=d.getWidth();
测量高度=d.getHeight();
}
setImageView(Measuredheight-Utils.dpToPx(120,EditReceiptActivity.this),Measuredwidth-Utils.dpToPx(60,EditReceiptActivity.this),
颜色(透明);
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
loaderImageView.hideProgress();
设置图像位图(bmd);
图像.setBackgroundColor(颜色.透明);
//将图像居中
image.setScaleType(ScaleType.FIT_CENTER);
super.onPostExecute(结果);
}
}
尝试使用毕加索图书馆-