How to Assign background container for horizontal image scroll list

ⅰ亾dé卋堺 提交于 2020-01-04 07:18:12

问题


I want to display horizontal image scroll list so i search on google and i found very good tutorial but non of them explain idea to implement dynamic container for that horizontal list.

I want to display one image at the starting of horizontal list and one at the end of list.( like arrow image ) and i want to set image container for my image scroll list it is very confusing and specially fresher like me. I am stuck at this point to explain what i want exactly i am attaching one image and my current code.

Any help is appreciated. please Guide me . Thank you for valuable time.

Here is main activity code

MyHorizontalLayout myHorizontalLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myHorizontalLayout = (MyHorizontalLayout)findViewById(R.id.mygallery);
    String ExternalStorageDirectoryPath = Environment
            .getExternalStorageDirectory()
            .getAbsolutePath();
    String targetPath = ExternalStorageDirectoryPath + "/test/";
    Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
    File targetDirector = new File(targetPath);
     File[] files = targetDirector.listFiles();
    for (File file : files){
    myHorizontalLayout.add(file.getAbsolutePath());
    }    
}

Here is my HorizontalLayout code

public class MyHorizontalLayout extends LinearLayout {
Context myContext;
ArrayList<String> itemList = new ArrayList<String>();
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));  
}
ImageView getImageView(final int i){
    ImageView imageView = new ImageView(myContext);
    imageView.setLayoutParams(new LayoutParams(150, 150));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    Bitmap bm = null;
    if (i < itemList.size()){
        bm = decodeSampledBitmapFromUri(itemList.get(i), 150, 150);
    }
    imageView.setImageBitmap(bm);
    imageView.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(myContext, 
                    "Clicked - " + itemList.get(i), 
                    Toast.LENGTH_LONG).show();
        }});

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

Here is XML file

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp" >
    <com.example.test.MyHorizontalLayout
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />


回答1:


Use Relative layout as base put background to it(scroll view background), Inside this add your horizontal layout and two arrows using framelayout. Best of luck!



来源:https://stackoverflow.com/questions/15925068/how-to-assign-background-container-for-horizontal-image-scroll-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!