如何分配水平图像滚动列表背景容器 [英] How to Assign background container for horizontal image scroll list

查看:361
本文介绍了如何分配水平图像滚动列表背景容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示图像水平滚动列表,所以我对谷歌搜索,我发现非常好的教程,但他们不解释的想法实现动态容器的水平列表。

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.

我想在水平列表,一个在列表的末尾的起始来显示一个图像。(如箭头图像)
我想设置图像容器我的形象滚动列表
这是非常混乱,特别是新鲜的我一样。
我停留在这一点上解释正是我想要我附上一个图像和我目前的code。

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.

任何帮助是AP preciated。请指导我。
感谢您的宝贵时间。

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

下面是主要活动code

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

下面是我的Horizo​​ntalLayout code

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

下面是XML文件

<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" />

推荐答案

使用相对布局为基础提出的背景给它(滚动视图背景),这里面添加水平的布局和使用的FrameLayout两个箭头。祝你好运!

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!

这篇关于如何分配水平图像滚动列表背景容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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