gpt4 book ai didi

android - 根据 Imageviews 的高度和宽度在 onCreate() 中创建缩放位图

转载 作者:太空狗 更新时间:2023-10-29 12:48:37 25 4
gpt4 key购买 nike

这是我遇到的问题,在 onCreate() 中,我想最初将 imageview 设置为文件列表中的第一个图像,但是使用 ImageViews 宽度和高度,当它来回翻转时它设置图像到 ImageViews dimentions 没问题,但在 onCreate() 中我得到 IllegalArgumentException 宽度和高度必须大于 0。我试图创建启动方法,如果它没有发生在 onCreate() 中,idk,非常感谢任何帮助,谢谢你的时间

    public class ViewFlipperActivity extends Activity {

ViewFlipper page;

Animation animFlipInForeward;
Animation animFlipOutForeward;
Animation animFlipInBackward;
Animation animFlipOutBackward;
String[] imagefiles;
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "how");
int filescount,nowcount;
ImageView image;
Matrix matrix;
Bitmap d;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imagefiles = file.list();
filescount = imagefiles.length;
nowcount = 0;
matrix = new Matrix();
matrix.postRotate(90);
page = (ViewFlipper)findViewById(R.id.flipper);
image = (ImageView)findViewById(R.id.zero);

animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.left_in);
animFlipOutForeward = AnimationUtils.loadAnimation(this, R.anim.left_out);
animFlipInBackward = AnimationUtils.loadAnimation(this, R.anim.right_in);
animFlipOutBackward = AnimationUtils.loadAnimation(this, R.anim.right_out);
start();
}
private void start(){
d = BitmapFactory.decodeFile(file.toString() +"/" + imagefiles[nowcount]);

d = Bitmap.createBitmap(d, 0, 0, d.getWidth(), d.getHeight(), matrix, true);
System.gc();
d = Bitmap.createScaledBitmap(d, image.getWidth(), image.getHeight(), true);
System.gc();
image.setImageBitmap(d);
System.gc();
}

private void SwipeRight(){
page.setInAnimation(animFlipInBackward);
page.setOutAnimation(animFlipOutBackward);
nowcount--;
if(nowcount < 0)
nowcount = filescount - 1;
d = BitmapFactory.decodeFile(file.toString() +"/" + imagefiles[nowcount]);

d = Bitmap.createBitmap(d, 0, 0, d.getWidth(), d.getHeight(), matrix, true);
System.gc();
d = Bitmap.createScaledBitmap(d, image.getWidth(), image.getHeight(), true);
System.gc();
image.setImageBitmap(d);
System.gc();

page.showPrevious();
Log.d("show previous", "exe");
}

private void SwipeLeft(){
page.setInAnimation(animFlipInForeward);
page.setOutAnimation(animFlipOutForeward);
nowcount++;
if(nowcount > 3)
nowcount = 0;
d = BitmapFactory.decodeFile(file.toString() +"/" + imagefiles[nowcount]);

d = Bitmap.createBitmap(d, 0, 0, d.getWidth(), d.getHeight(), matrix, true);
System.gc();
d = Bitmap.createScaledBitmap(d, image.getWidth(), image.getHeight(), true);
System.gc();
image.setImageBitmap(d);
System.gc();
page.showNext();
Log.d("show next", "exe");
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}

SimpleOnGestureListener simpleOnGestureListener
= new SimpleOnGestureListener(){

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

float sensitvity = 50;
if((e1.getX() - e2.getX()) > sensitvity){
SwipeLeft();
}else if((e2.getX() - e1.getX()) > sensitvity){
SwipeRight();
}

return true;
}

};

GestureDetector gestureDetector
= new GestureDetector(simpleOnGestureListener);
}

最佳答案

ImageView 的高度和宽度只有在为父 View 请求布局时才会计算,这是在 onCreate 完成之后。你应该实现一个 OnGlobalLayoutListener并从那里获取 ImageView 的高度和宽度。不过,您不应该在其中加载图像,而是使用 AsyncTask 或其他方法将其加载到后台。

关于android - 根据 Imageviews 的高度和宽度在 onCreate() 中创建缩放位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15074578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com