我正在构建一个显示两个水平滚动视图的活动。顶部的图像保存大图像,而较低的图像保存这些图像的缩略图。当用户点击缩略图时,上部滚动视图将自动滚动到与缩略图对应的图像。我的问题是布局的getWidth()方法总是返回0.我在运行时创建布局然后调用getwidth()方法,所以它应该返回正确的值,但它不是。
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);
}
}
}
我正在创建布局的insertPhoto函数 -
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;
}
提前感谢您的帮助。
编辑:我只发布需要宽度的代码部分。我修改它以使用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();
}
});
答案 0 :(得分:0)
在使用get width method-
之前尝试使用此代码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();
}
});