根据Imageviews高度和宽度中创建的onCreate(经缩放的位图) [英] creating a scaled bitmap in onCreate() according to the Imageviews height and width

查看:226
本文介绍了根据Imageviews高度和宽度中创建的onCreate(经缩放的位图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是问题即时具有,在OnCreate()我想最初设置的ImageView到第一图像中的文件的列表,但与ImageViews宽度和高度,当它翻转来回它设置图像到ImageViews dimentions在OnCreate()我得到抛出:IllegalArgumentException宽度和高度必须大于0没有问题,但我试图使起动方法,如果因为它不是在OnCreate发生(),IDK的,任何帮助是极大的$ AP p $ pciated,感谢您的时间

 公共类ViewFlipperActivity延伸活动{   ViewFlipper页面;   动画animFlipInForeward;
   动画animFlipOutForeward;
   动画animFlipInBackward;
   动画animFlipOutBackward;
   的String []的图像文件;
   档案文件=新的文件(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES),如何);
   INT filescount,nowcount;
   ImageView的形象;
   矩阵矩阵;
   位图D组;   / **当第一次创建活动调用。 * /
   @覆盖
   公共无效的onCreate(捆绑savedInstanceState){
   super.onCreate(savedInstanceState);
   的setContentView(R.layout.main);
   图像文件中的File.List =();
   filescount = imagefiles.length;
   nowcount = 0;
   矩阵=新的Matrix();
   matrix.postRotate(90);
   网页=(ViewFlipper)findViewById(R.id.flipper);
   图像=(ImageView的)findViewById(R.id.zero);   animFlipInForeward = AnimationUtils.loadAnimation(这一点,R.anim.left_in);
   animFlipOutForeward = AnimationUtils.loadAnimation(这一点,R.anim.left_out);
   animFlipInBackward = AnimationUtils.loadAnimation(这一点,R.anim.right_in);
   animFlipOutBackward = AnimationUtils.loadAnimation(这一点,R.anim.right_out);
   开始();
   }
   私人无效的start(){
   D = BitmapFactory.de codeFILE(file.toString()+/+的图像文件[nowcount]);   D = Bitmap.createBitmap(D,0,0,d.getWidth(),d.getHeight(),矩阵,真);
   System.gc()的;
   D = Bitmap.createScaledBitmap(D,image.getWidth(),image.getHeight(),TRUE);
   System.gc()的;
   image.setImageBitmap(四);
   System.gc()的;
   }   私人无效SwipeRight(){
   page.setInAnimation(animFlipInBackward);
   page.setOutAnimation(animFlipOutBackward);
   nowcount--;
   如果(nowcount℃,)
 nowcount = filescount - 1;
   D = BitmapFactory.de codeFILE(file.toString()+/+的图像文件[nowcount]);   D = Bitmap.createBitmap(D,0,0,d.getWidth(),d.getHeight(),矩阵,真);
   System.gc()的;
   D = Bitmap.createScaledBitmap(D,image.getWidth(),image.getHeight(),TRUE);
   System.gc()的;
   image.setImageBitmap(四);
   System.gc()的;   page.show previous();
   Log.d(显示previous,EXE);
   }   私人无效SwipeLeft(){
   page.setInAnimation(animFlipInForeward);
   page.setOutAnimation(animFlipOutForeward);
   nowcount ++;
   如果(nowcount→3)
 nowcount = 0;
   D = BitmapFactory.de codeFILE(file.toString()+/+的图像文件[nowcount]);   D = Bitmap.createBitmap(D,0,0,d.getWidth(),d.getHeight(),矩阵,真);
   System.gc()的;
   D = Bitmap.createScaledBitmap(D,image.getWidth(),image.getHeight(),TRUE);
   System.gc()的;
   image.setImageBitmap(四);
   System.gc()的;
   page.showNext();
   Log.d(显示下一个,执行);
   }  @覆盖
  公共布尔onTouchEvent(MotionEvent事件){
   // TODO自动生成方法存根
  返回gestureDetector.onTouchEvent(事件);
  }  simpleOnGestureListener simpleOnGestureListener
  =新SimpleOnGestureListener(){  @覆盖
   公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
  浮动velocityY){  浮sensitvity = 50;
  如果((e1.getX() - e2.getX())> sensitvity){
   SwipeLeft();
  }否则如果((e2.getX() - e1.getX())> sensitvity){
  SwipeRight();
  }  返回true;
  }  };  gestureDetector gestureDetector
  =新GestureDetector(simpleOnGestureListener);
  }


当请求父视图,这是继的onCreate <布局

解决方案

的高度和图像视图的宽度将只计算/ code>已完成。您应该实施<一个href=\"http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html\"相对=nofollow> OnGlobalLayoutListener 并从那里得到的ImageView的高度和宽度。你不应该载入您的形象在里面,不过,使用的AsyncTask 或一些其他的方式来加载它的背景。

Here is the problem im having, in the onCreate() i would like to initially set the imageview to the first image in the list of files but with the ImageViews width and height, when it flips back and forth it sets the image to the ImageViews dimentions no problem but in the onCreate() i get IllegalArgumentException width and height must be greater than 0. i tried to make the start method if as it wasnt happening in the onCreate(), idk, any help is greatly appreciated, thank you for your time

    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);
  }

解决方案

The height and width of the image view will only be calculated when a layout is requested for the parent view, which is after onCreate has finished. You should implement an OnGlobalLayoutListener and get the imageview's height and width from there. You shouldn't load your image in there, though, use an AsyncTask or some other approach to load it on the background.

这篇关于根据Imageviews高度和宽度中创建的onCreate(经缩放的位图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆