2014-04-26 104 views
0

我正在構建一個顯示兩個水平滾動視圖的活動。最上面的一個保存大圖像,而下面的那個保存那些圖像的縮略圖。當用戶點擊縮略圖時,上滾動視圖將自動滾動到與縮略圖對應的圖像。我的問題是,佈局的getWidth()方法總是返回0.我在運行時創建佈局,然後調用getwidth()方法,所以它應該返回正確的值,但它不是。getWidth()方法返回0

public class ZoomActivity extends Activity { 
    private final String THUMBS="http://kurdshopping.net/thumbs/"; 
    private final String UPLOADS="http://kurdshopping.net/uploads/"; 
    private ImageView imageView; 
    private String path; 
    private String[] filenames; 
    private LinearLayout l1,l2; 
    private int[] widths=null; 
    private HorizontalScrollView hsv=null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
     setContentView(R.layout.activity_zoom); 

     ActionBarUtils.setActionBar(this); 

     path = getIntent().getExtras().getString("path"); 
     filenames=getIntent().getExtras().getStringArray("images"); 
     widths=new int[filenames.length]; 
     l1=(LinearLayout) findViewById(R.id.LinearLayout1); 
     l2=(LinearLayout) findViewById(R.id.LinearLayout2); 
     hsv=(HorizontalScrollView) findViewById(R.id.horizontalScrollView1); 

     for(int i=0; i<filenames.length; i++){ 
      BitmapLoaderTask task = new BitmapLoaderTask(i); 
      task.execute(UPLOADS+filenames[i],"1"); 
     } 
     for(int i=0;i<filenames.length;i++){ 
      BitmapLoaderTask task = new BitmapLoaderTask(i); 
      task.execute(THUMBS+filenames[i],"2"); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.zoom, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      finish(); 
      break; 
//  case R.id.action_all: 
//   Intent allitemIntent = new Intent(MainActivity.this, 
//     AllItemsActivity.class); 
//   startActivity(allitemIntent); 
//   break; 
//  case R.id.action_search: 
//   Intent search = new Intent(this, SearchDialogActivity.class); 
//   startActivity(search); 
//   break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    private void setImageBitmap(Bitmap bmp) { 
     imageView = new ScrollableImageView(this); 
     imageView.setLayoutParams(new LayoutParams(bmp.getWidth(), bmp 
       .getHeight())); 
     imageView.setImageBitmap(bmp); 
     ViewGroup root = (ViewGroup) findViewById(android.R.id.content); 
     root.addView(imageView); 
    } 

    private class BitmapLoaderTask extends AsyncTask<String, Void, Bitmap> { 
     private String layout; 
     private int index; 
     private int x_coords=0; 
     public BitmapLoaderTask(int index){ 
      super(); 
      this.index=index; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      setProgressBarIndeterminateVisibility(true); 
     } 

     @Override 
     protected synchronized Bitmap doInBackground(String... params) { 
//   AssetManager assets = getAssets(); 
      Bitmap bmp = null; 
      layout=params[1]; 
      try { 
       URL url = new URL(params[0]); 
       bmp = BitmapFactory.decodeStream(url.openConnection() 
         .getInputStream()); 

      } catch (IOException e) { 
       Log.e("ZoomActivity", e.getMessage(), e); 
      } 
      return bmp; 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      super.onPostExecute(result); 
      View view=null; 
      try { 
       view=ImageUtils.insertPhoto(ZoomActivity.this, result); 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if(layout.equals("1")){ 
       l1.addView(view); 
       view.setTag(x_coords); 
       x_coords+=((LinearLayout)view).getMeasuredWidth();// this is always returning 0, view.getLayoutParams().width is also same. 
      }else{ 
       l2.addView(view); 
       ImageView img=(ImageView) ((LinearLayout)view).getChildAt(0); 
       img.setTag(index); 
       img.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         int index=(Integer) v.getTag(); 
         LinearLayout layout = (LinearLayout) l1.getChildAt(index); 
         int x=(Integer) layout.getTag(); 
         hsv.scrollTo(x, 0); 
        } 
       }); 
      } 
//   ImageView imageview=(ImageView) findViewById(R.id.imageView1); 
//   imageview.setImageBitmap(result); 
//   setImageBitmap(result); 
      setProgressBarIndeterminateVisibility(false); 
     } 
    } 
} 

那裏我事先創建於版圖

public static View insertPhoto(final Activity activity, Bitmap bm) throws MalformedURLException, IOException { 
     LinearLayout layout = new LinearLayout(activity.getApplicationContext()); 
     layout.setLayoutParams(new LayoutParams(bm.getWidth()+10, bm.getHeight()+10)); 
     layout.setGravity(Gravity.CENTER); 

     final ImageView imageView = new ImageView(
       activity.getApplicationContext()); 
     imageView.setLayoutParams(new LayoutParams(bm.getWidth(), bm.getHeight())); 
     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     imageView.setImageBitmap(bm); 
//  imageView.setTag(filename); 

     layout.addView(imageView); 
     return layout; 
    } 

感謝您的幫助的insertPhoto功能。

編輯:我只發佈需要寬度的代碼部分。我修改它使用viewtreeoserver,但它仍然無法正常工作 -

l1.addView(view); 
        view.setTag(x_coords); 
        view.requestLayout(); 
        ViewTreeObserver vto=view.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

         @Override 
         public void onGlobalLayout() { 
          x_coords+=((LinearLayout)view).getMeasuredWidth(); 
         } 
        }); 
+1

http://stackoverflow.com/a/18272220 – Sree

+0

卻沒有回答SREE放棄工作嗎? – AndyFaizan

+0

嘗試給出的答案** [這裏](http://stackoverflow.com/questions/3591784/android-get-width-returns-0)**。 – AndyFaizan

回答

0

之前使用get寬度方法 -

yourlayout.requestLayout(); 

希望這有助於你試試這個代碼!

如果它不工作,請讓我知道我會盡力幫助你。

編輯答案

試試這個代碼 -

l1.addView(view); 
        view.setTag(x_coords); 
        view.requestLayout(); 
        ViewTreeObserver vto=view.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

         @Override 
         public void onGlobalLayout() { 
              view.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
          x_coords+=((LinearLayout)view).getMeasuredWidth(); 
         } 
        }); 
+0

它仍然返回0. – Pankaj

+0

你可以發佈你的代碼片段只爲獲得寬度?你想獲得寬度的位置? –

+0

請嘗試使用「l1」的getwidth,這是您的線性佈局,我認爲 –