如何设置图像的horizo​​ntalScrollView的对齐

时间:2015-12-22 05:13:01

标签: java android horizontalscrollview

我使用horizo​​ntalscrollview以水平方式创建了图像列表,它的工作正常,但我想显示最后的图像,因为它的捕获图像。 我的布局

<HorizontalScrollView
                android:id="@+id/horizontal_scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 >

                <com.activities.MyHorizontalLayout
                    android:id="@+id/linear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" 
                    />

 </HorizontalScrollView>

Horizo​​ntalScrollView类

public class MyHorizontalLayout extends LinearLayout {

Context myContext;
ArrayList<String> itemList = new ArrayList<String>();
public Bitmap selectedbm;
public static String selectedfile;

public MyHorizontalLayout(Context context) {
    super(context);
    myContext = context;
}

public MyHorizontalLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    myContext = context;
}

public MyHorizontalLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    myContext = context;
}

void add(String path) { 
    int newIdx = itemList.size();
    itemList.add(path);
    addView(getImageView(newIdx));

}

void remove(String path,int ind) {  

    removeView(getImageView(ind));
    itemList.remove(ind);


}


ImageView getImageView(final int i) {
    Bitmap bm = null;
    if (i < itemList.size()) {
        bm = decodeSampledBitmapFromUri(itemList.get(i), 220, 220);
    }

    ImageView imageView = new ImageView(myContext);
    imageView.setLayoutParams(new LayoutParams(180, 220));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setImageBitmap(bm);
    /*ImageGalleryEdit.edit.setVisibility(View.GONE);
    ImageGalleryEdit.delete.setVisibility(View.GONE);
    */
    ImageGalleryEdit.viewimg.setImageBitmap(decodeSampledBitmapFromUri(itemList.get(i), 1220, 1220));
    selectedfile = itemList.get(i);

    imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ImageGalleryEdit.edit.setVisibility(View.VISIBLE);
            ImageGalleryEdit.delete.setVisibility(View.VISIBLE);

            ImageGalleryEdit.viewimg.setImageBitmap(decodeSampledBitmapFromUri(itemList.get(i), 1220, 1220));
            selectedbm = decodeSampledBitmapFromUri(itemList.get(i), 1220, 1220);
            selectedfile = itemList.get(i);

        }
    });

    return imageView;
}

public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
        int reqHeight) {
    Bitmap bm = null;

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }

    return inSampleSize;
  }
 }

1 个答案:

答案 0 :(得分:2)

在将数据填充到horizo​​ntalscrollview之后尝试使用fullScroll属性:

hs.postDelayed(new Runnable() {
    public void run() {
        hs.fullScroll(View.FOCUS_RIGHT);
    }
}, 100);