如何在Android中选取图片框? [英] How to do marquee of images in Android?

查看:88
本文介绍了如何在Android中选取图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手,现在我试图将图像放置在水平滚动视图中.

I am new to Android programming, now I am trying to place images inside the horizontal scrollview.

实际上,我想要显示的是要自动从右向左滚动的图像阵列.

Actually what I am trying to do is, I want to display array of images which needs to be scrolling automatically from right to left.

到目前为止,我已经尝试过以下方法

So far I have tried as below

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView 
        android:id="@+id/image2"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/icon"
        android:layout_centerHorizontal="true"
        android:layout_marginRight="5px"/>
</LinearLayout>
</HorizontalScroll>

活动文件-

package com.myhorizontalScroll;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

public class MyHorizontalScrollActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // declare a class field:
            final Handler h = new Handler();

            // later:
            final ImageView scroll=(ImageView)findViewById(R.id.image2);
            Thread t = new Thread(){
                public void run(){
                    int y = scroll.getScrollY();
                    int x = scroll.getScrollX();

                    while(y<1600){
                        // need final values to create anonymous inner class
                        final int X = x;
                        final int Y = y;
                        h.post(new Runnable() {
                            public void run() {
                                scroll.scrollTo(X, Y);
                            }
                        });
                        x++;
                        try {
                            sleep(1000/12);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            };
            t.start();
    }
}

现在,图像视图从左到左; imageview实际上位于左侧.

Now the imageview goes from left to left; the imageview actually positioned in the left side.

如何将imageview放置在右侧,我希望此滚动需要连续完成.

How to place the imageview in right side and I want this scrolling needs to done continuously.

我该如何进行呢?

推荐答案

这是我在其中一个应用程序中执行的操作.这本质上用于显示喜欢帖子的用户的个人资料图片.您自然会为自己弄清楚一点,但是我会尽力帮助您.

This is how I do it in one of my apps. This is essentially used to display the Profile Pictures of User's who like a post. You will naturally have to figure out a bit for yourself, but I will try and help where I can.

首先,XML:

<LinearLayout
    android:id="@+id/linlaLikesStrip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:text="LIKES BY:"
        android:textColor="#888888"
        android:textSize="14sp"
        android:textStyle="bold" >
    </TextView>

    <HorizontalScrollView
        android:id="@+id/horscoLikes"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:overScrollMode="never"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/linlaLikes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

在我的Java代码中,我声明并在全局范围内进行了强制转换,因此您需要做一点点弄清楚.

In my Java code, I declare and cast them globally, so you will have to do a little figuring out.

JAVA代码:

linlaLikes.removeAllViews();

for (int i = 0; i < arrLikes.size(); i++) {

    final getLikes newLikes = arrLikes.get(i);

    ImageView imgUsers = new ImageView(FeedDetails.this);

    // SET THE IMAGEVIEW DIMENSIONS
    int dimens = 45;
    float density = getResources().getDisplayMetrics().density;
    int finalDimens = (int)(dimens * density);

    LinearLayout.LayoutParams imgvwDimens = new LinearLayout.LayoutParams(finalDimens, finalDimens);
    imgUsers.setLayoutParams(imgvwDimens);

    // SET SCALETYPE
    imgUsers.setScaleType(ScaleType.CENTER_CROP);

    // SET THE MARGIN
    int dimensMargin = 4;
    float densityMargin = getResources().getDisplayMetrics().density;
    int finalDimensMargin = (int)(dimensMargin * densityMargin);

    LinearLayout.LayoutParams imgvwMargin = new LinearLayout.LayoutParams(finalDimens, finalDimens);
    imgvwMargin.setMargins(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);

    // SET PADDING
//              imgUsers.setPadding(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);

    // DISPLAY THE IMAGES USING THE PROFILELOADER CLASS
    proLoader.DisplayImage(newLikes.getUserProfilePicture(), imgUsers);

    // ADD THE NEW IMAGEVIEW WITH THE PROFILE PICTURE LOADED TO THE LINEARLAYOUT
    linlaLikes.addView(imgUsers, imgvwMargin);

    imgUsers.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent showProfile = new Intent(getApplicationContext(), WallPost.class);
            showProfile.putExtra("USER_ID", newLikes.getUserID());
            showProfile.putExtra("USER_PROFILE", "USER_PROFILE");
            startActivity(showProfile);
        }
    });
}

一些解释:

  1. arrLikes是保存个人资料图片的ArrayList.我在for loop中使用arrLikes.size()来获取正确的数字.
  2. 我没有在XML中对ImageView进行硬编码(显然,因为所需的数目未知),而是在运行时动态创建了必要的数目.
  3. 然后要进行很多工作,以设置ImageViews的大小,然后在它们之间设置Margin.
  4. 最后,我使用@Fedor的延迟加载在新创建的ImageViews中显示个人资料图片.
  1. arrLikes is an ArrayList that holds the Profile Picture's. I use arrLikes.size() in the for loop to get the correct number.
  2. Instead of hard-coding ImageViews (obviously, because the number needed is unknown) in the XML, I dynamically create the necessary number at run-time.
  3. Then follows a lot of stuff for setting up the size of the ImageViews and then setting the Margin between them.
  4. And finally, I am using @Fedor's Lazy Loading to display the Profile Pictures in the newly created ImageViews.

希望这会有所帮助.并随时询问您在上述任何方面是否有任何困难.

Hope this helps. And feel free to ask if you have any difficulties with any of the above.

这篇关于如何在Android中选取图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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