Android的 - 如何做图像选取框 [英] Android - How to do Marquee of images
问题描述
我是新来的Android程序,现在我想将水平scrolview内部的图像。
I am new to android Programming, now i am trying to place images inside the horizontal scrolview.
其实我所要做的是,我想显示阵列图像需要被从右到左自动滚动。
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的推移从左到左。所述imageview的实际定位在左侧。
Now the imageview goes from left to left. the imageview actually positioned in the left side .
请帮我如何放置在右侧的ImageView的,我想这个滚动需要连续进行。
please help me out how to place the imageview in right side and i want this scrolling needs to done continuously.
请帮我如何我可以继续说。
Please help me out how i can proceed with that.
推荐答案
这是我如何做到这一点,在我的应用程序之一。这本质上是用来显示用户的个人资料图片谁喜欢一个职位。你自然会要弄清楚一点为自己,但我会尽力帮助在那里我可以。
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 code,我宣布和全球扮演他们,所以你必须做一些搞清楚。
In my Java code, I declare and cast them globally, so you will have to do a little figuring out.
Java的code:
The JAVA Code:
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);
}
});
}
这一点解释:
-
arrLikes
是的ArrayList
持有该头像的。我用arrLikes.size()
在循环
,以获得正确的数字。 - 代替硬编码ImageViews(显然,因为所需要的数量是未知的),在XML中,我动态地创建在运行时所需的数量。
- 然后遵循了很多东西用于设置
ImageViews
的大小,然后在它们之间设置保证金
- 最后,我正在使用@费多尔的延迟加载,以显示我的照片在新创建
ImageViews
。
arrLikes
is anArrayList
that holds the Profile Picture's. I usearrLikes.size()
in thefor loop
to get the correct number.- Instead of hard-coding ImageViews (obviously, because the number needed is unknown) in the XML, I dynamically create the necessary number at run-time.
- Then follows a lot of stuff for setting up the size of the
ImageViews
and then setting theMargin
between them. - 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屋!