将imageview设置为水平滚动视图

时间:2016-03-11 05:34:32

标签: android android-imageview horizontalscrollview

您好我有一个网址,它给了我2个图片链接。我必须下载图像并设置图像。我正在使用水平滚动视图,直到获取图像URL并使用异步任务下载图像。有人可以让我知道如何将图像设置为水平滚动视图。

public void fetchImageUrl(){
    String url="http://apps.aspelec.com/cms/api/UploadImageAndChangeComplaintStatus?companyid=1&callrecordid=13";
    StringRequest request=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            System.out.println(response);
            getJsonUrl(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("ErrorListner=",error.getMessage());
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(request);

}
public void getJsonUrl(String response){
    try {
        JSONArray jArray = new JSONArray(response);
        ArrayList<String> imgurl=new ArrayList<>();
        for(int i=0;i<1;i++){
          String dummyurl=jArray.getJSONObject(i).getString("ImagePath");
            System.out.println("dummy url=="+dummyurl);
            imgurl.add(dummyurl);
            setView(imgurl);
        }
    }catch (JSONException e){

    }
}
public void setView(ArrayList<String> imgUrl){
    for(int j=0;j<imgUrl.size();j++){
        vi=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v=vi.inflate(R.layout.custome_serverdownloadedimageview,null,false);
        img=(ImageView)v.findViewById(R.id.customserverdownlodedimg);
        new DownloadImage(img).execute(imgUrl.get(j));
        //Picasso.with(this).load(imgUrl.get(j)).resize(100,100).into(img);
    }
}

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/supervisor_complaint_details_cardview"
    android:fillViewport="true"
    android:foregroundGravity="left"
    android:paddingBottom="25dp"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    android:paddingTop="25dp"
    tools:context="com.six30labs.cms.activity.VerifyDetailSupervisor">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/layoutComplaintNameSupervisor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--LABEL FOR COMPLAINT NAME-->
        <TextView
            android:id="@+id/verifyComplaintLabelSupervisor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="@string/complaint_name"
            android:textColor="@color/colorPrimary"
            android:textSize="17sp"
            android:typeface="serif" />


        <!--TEXT FIELD FOR COMPLAINT-->
        <TextView
            android:id="@+id/verifyComplaintFieldSupervisor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/verifyComplaintLabelSupervisor"
            android:layout_marginTop="5dp"
            android:gravity="left"
            android:maxLines="5"
            android:minLines="2"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="normal"
            android:typeface="serif" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/verifyComplaintFieldSupervisor"
            android:layout_marginTop="10dp"
            android:background="#000000" />

    </RelativeLayout>

    <!--TICKET NUMBER-->
    <RelativeLayout
        android:id="@+id/layoutTicketNumberSupervisor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layoutComplaintNameSupervisor">
        <!--LABEL FOR TICKET NUMBER-->
        <TextView
            android:id="@+id/verifyLabelTicketNoSupervisor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:gravity="left"
            android:text="@string/ticket_number"
            android:textColor="@color/colorPrimary"
            android:textSize="17sp"
            android:typeface="serif" />
        <!--TEXT FIELD FOR TICKET NUMBER-->
        <TextView
            android:id="@+id/verifyTicketNumberFieldSupervisor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/verifyLabelTicketNoSupervisor"
            android:layout_marginTop="10dp"
            android:gravity="left"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="normal"
            android:typeface="serif" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/verifyTicketNumberFieldSupervisor"
            android:layout_marginTop="10dp"
            android:background="#000000" />


    </RelativeLayout>

        <!--Assigned Date & Due Date-->
        <LinearLayout

            android:id="@+id/layoutAssignedAndDueDateSupervisor"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1"
            android:layout_below="@+id/layoutTicketNumberSupervisor">

            <RelativeLayout
                android:id="@+id/layoutAssignedDateSupervisor"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5">

                <!--LABEL FOR Assigned DATE-->
                <TextView
                    android:id="@+id/verifylabelAssignedDateSupervisor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:gravity="left"
                    android:text="@string/assigned_date"
                    android:textColor="@color/colorPrimary"
                    android:textSize="17sp"
                    android:typeface="serif" />

                <!--TEXT FIELD FOR Assigned DATE-->
                <TextView
                    android:id="@+id/VerifyAssignedDateFieldSupervisor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/verifylabelAssignedDateSupervisor"
                    android:layout_marginTop="10dp"
                    android:gravity="left"
                    android:textColor="@color/black"
                    android:textSize="13sp"
                    android:textStyle="normal"
                    android:typeface="serif" />
                </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layoutDueDateSupervisor"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5">

                <!--Label for due date-->

                <TextView
                    android:id="@+id/verifylabelDueDateSupervisor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:layout_centerHorizontal="true"
                    android:text="@string/due_date"
                    android:textColor="@color/colorPrimary"
                    android:textSize="17sp"
                    android:typeface="serif" />

                <!--Text field for due date-->
                <TextView
                    android:id="@+id/VerifyDueDateFieldSupervisor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/verifylabelDueDateSupervisor"
                    android:layout_marginTop="10dp"
                    android:gravity="left"
                    android:textColor="@color/black"
                    android:textSize="13sp"
                    android:textStyle="normal"
                    android:typeface="serif" />
            </RelativeLayout>
        </LinearLayout>

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/layoutAssignedAndDueDateSupervisor"
            android:layout_marginTop="10dp"
            android:background="#000000" />

        <!--Completed Date & Priority-->
        <LinearLayout
            android:id="@+id/layoutCompletedDateAndPrioritySupervisor"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1"
            android:layout_below="@+id/divider">

            <RelativeLayout
                android:id="@+id/layoutCompletedDateSupervisor"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5">

                <!--LABEL FOR Completed DATE-->
                <TextView
                    android:id="@+id/verifylabelCompletedDateSupervisor"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:text="@string/completed_date"
                    android:textColor="@color/colorPrimary"
                    android:textSize="17sp"
                    android:typeface="serif" />

                <!--TEXT FIELD FOR completed DATE-->
                <TextView
                    android:id="@+id/VerifyCompletedDateFieldSupervisor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/verifylabelCompletedDateSupervisor"
                    android:layout_marginTop="10dp"
                    android:gravity="left"
                    android:textColor="@color/black"
                    android:textSize="13sp"
                    android:textStyle="normal"
                    android:typeface="serif" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5">

                <ImageView
                    android:id="@+id/verifyPriorityImage"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    android:src="@mipmap/calendar"/>
            </RelativeLayout>
        </LinearLayout>

        <View
            android:id="@+id/divider1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/layoutCompletedDateAndPrioritySupervisor"
            android:layout_marginTop="10dp"
            android:background="#000000" />

        <RelativeLayout
            android:id="@+id/layoutVerifyServerImageLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/divider1">

            <TextView
                android:id="@+id/serverImageLabel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/view_Image"
                android:layout_marginTop="15dp"
                android:textColor="@color/colorPrimary"
                android:textSize="17sp"
                android:typeface="serif"/>

        </RelativeLayout>

        <HorizontalScrollView
            android:id="@+id/layoutVerifyServerImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="horizontal"
            android:layout_below="@+id/layoutVerifyServerImageLabel">

            <LinearLayout
                android:id="@+id/serverDownloadedImageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"/>

        </HorizontalScrollView>
    </RelativeLayout>
</ScrollView>

自定义图像视图

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/customserverdownlodedimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

你可以做这样的事情

 horizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontal_grid);
        int[] images = new int[]{R.drawable.image1, R.drawable.image2, ....R.drawable.image10};
        LinearLayout sv = (LinearLayout) findViewById (R.id.horizontal_container);
        for (int i=0 ; i<images.length; i++){
            ImageView iv = new ImageView (this);
            LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  ViewGroup.LayoutParams.MATCH_PARENT);
            iv.setPadding(4,0,0,0);
            iv.setImageResource(images[i]);
            iv.setLayoutParams(imageViewLayoutParams);
            sv.addView(iv);
        }

你的xml看起来应该是这样的

    <HorizontalScrollView
        android:id="@+id/horizontal_grid"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="40">
        <LinearLayout
            android:id="@+id/horizontal_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
        </LinearLayout>

    </HorizontalScrollView>

答案 1 :(得分:0)

public void setView(ArrayList<String> imgUrl){

serverDownloadedImageView = (LinearLayout)findViewById(R.id.serverDownloadedImageView);
serverDownloadedImageView.removeAllViews();
    for(int j=0;j<imgUrl.size();j++){

     ImageView img = new ImageView(this);

        new DownloadImage(img).execute(imgUrl.get(j));
        //Picasso.with(this).load(imgUrl.get(j)).resize(100,100).into(img);
    serverDownloadedImageView.addView(img,j)

    }
}