Настройка HorizontalScrollView
Я хочу получить изображение ниже. Итак, я работаю с HorizontalScrollView, чтобы получить эффекты карусели. Я пытался с Coverflow Library. Но он использует галерею, которая устарела в API 16.
Я достиг, как следовать и столкнулся с проблемой, как:
- При запуске программы все изображения не отображаются небольшими по размеру. После касания появляется реальный размер, который я установил в коде 300*300.
- Невозможно установить желаемую позицию просмотра текста и просмотра изображения.
Я использовал класс CenteringHor horizontalScrollView
public class CenteringHorizontalScrollView extends HorizontalScrollView
implements View.OnTouchListener {
private Context mContext;
private static final int SWIPE_PAGE_ON_FACTOR = 10;
private int mActiveItem;
private float mPrevScrollX;
private boolean mStart;
private int mItemWidth;
View targetLeft, targetRight;
ImageView leftImage, rightImage;
RelativeLayout leftFrame, rightFrame;
public CenteringHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mItemWidth = 100; // or whatever your item width is.
setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
boolean handled = false;
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (mStart) {
mPrevScrollX = x;
mStart = false;
}
break;
case MotionEvent.ACTION_UP:
mStart = true;
int minFactor = mItemWidth / SWIPE_PAGE_ON_FACTOR;
if ((mPrevScrollX - (float) x) > minFactor) {
if (mActiveItem < getMaxItemCount() - 1) {
mActiveItem = mActiveItem + 1;
}
} else if (((float) x - mPrevScrollX) > minFactor) {
if (mActiveItem > 0) {
mActiveItem = mActiveItem - 1;
}
}
scrollToActiveItem();
handled = true;
break;
}
return handled;
}
private int getMaxItemCount() {
return ((LinearLayout) getChildAt(0)).getChildCount();
}
private LinearLayout getLinearLayout() {
return (LinearLayout) getChildAt(0);
}
/**
* Centers the current view the best it can.
*/
public void centerCurrentItem() {
if (getMaxItemCount() == 0) {
return;
}
int currentX = getScrollX();
View targetChild;
int currentChild = -1;
do {
currentChild++;
targetChild = getLinearLayout().getChildAt(currentChild);
} while (currentChild < getMaxItemCount()
&& targetChild.getLeft() < currentX);
if (mActiveItem != currentChild) {
mActiveItem = currentChild;
scrollToActiveItem();
}
}
private void scrollToActiveItem() {
int maxItemCount = getMaxItemCount();
if (maxItemCount == 0) {
return;
}
int targetItem = Math.min(maxItemCount - 1, mActiveItem);
targetItem = Math.max(0, targetItem);
mActiveItem = targetItem;
// Scroll so that the target child is centered
View targetView = getLinearLayout().getChildAt(targetItem);
RelativeLayout centerFrame = (RelativeLayout) targetView;
ImageView centerImage = (ImageView) centerFrame.getChildAt(0);
TextView centerTextView = (TextView) centerFrame.getChildAt(1);
int height = 300;// set size of centered image
int widths = 300;
// LinearLayout.LayoutParams flparams = new
// LinearLayout.LayoutParams(height, height);
RelativeLayout.LayoutParams flparams = new RelativeLayout.LayoutParams(
widths, height);
centerImage.setLayoutParams(flparams);
centerTextView.setLayoutParams(flparams);
flparams.setMargins(50, 30, 50, 0);
// get the image to left of the centered image
if ((targetItem - 1) >= 0) {
targetLeft = getLinearLayout().getChildAt(targetItem - 1);
leftFrame = (RelativeLayout) targetLeft;
leftImage = (ImageView) leftFrame.getChildAt(0);
int width = 250;// set the size of left image
RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(
width, width);
leftParams.setMargins(50, 30, 50, 0);
leftImage.setLayoutParams(leftParams);
}
// get the image to right of the centered image
if ((targetItem + 1) < maxItemCount) {
targetRight = getLinearLayout().getChildAt(targetItem + 1);
rightFrame = (RelativeLayout) targetRight;
rightImage = (ImageView) rightFrame.getChildAt(0);
int width = 250;// set the size of right image
RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(
width, width);
rightParams.setMargins(50, 30, 50, 0);
rightImage.setLayoutParams(rightParams);
}
int targetLeft = targetView.getLeft();
int childWidth = targetView.getRight() - targetLeft;
int width = getWidth() - getPaddingLeft() - getPaddingRight();
int targetScroll = targetLeft - ((width - childWidth) / 2);
super.smoothScrollTo(targetScroll, 0);
}
/**
* Sets the current item and centers it.
*
* @param currentItem
* The new current item.
*/
public void setCurrentItemAndCenter(int currentItem) {
mActiveItem = currentItem;
scrollToActiveItem();
}
} Мой TestHor horizontalScrollViewActivity выглядит так:
public class TestHorizontalScrollViewActivity extends Activity {
LinearLayout imageGallery;
Integer[] pics = { R.drawable.ic_launcher, R.drawable.android__logo_froyo,
R.drawable.android_eclair_logo, R.drawable.android_ics_logo,
R.drawable.android_jellybean_logo,
R.drawable.android_gingerbread_logo,
};
String[] texts = { "Hello", "Hi", "Welcome", "World", "Man", "Wan" };
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_scrollview);
imageGallery = (LinearLayout) findViewById(R.id.linearImage);
for (int i = 0; i < pics.length; i++) {
RelativeLayout imageFrame = new RelativeLayout(
TestHorizontalScrollViewActivity.this);
TextView titleText = new TextView(
TestHorizontalScrollViewActivity.this);
titleText.setText(texts[i]);
ImageView image = new ImageView(
TestHorizontalScrollViewActivity.this);
image.setBackgroundResource(pics[i]);
// here you must set the layout properties of both textView and
// imageView for getting the textView above imageView (I have no
// time to do it now, may be, will edit code some other day)
imageFrame.addView(image);
imageFrame.addView(titleText);
imageGallery.addView(imageFrame);
}
}
}
& Activity_horizont_scrollview.xml выглядит так
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.codencoder.ui.CenteringHorizontalScrollView
android:id="@+id/HSVImage"
android:layout_width="350dp"
android:layout_height="250dp"
android:layout_centerVertical="true" >
<LinearLayout
android:id="@+id/linearImage"
android:layout_width="350dp"
android:layout_height="250dp"
android:orientation="horizontal" >
</LinearLayout>
</com.codencoder.ui.CenteringHorizontalScrollView>
</RelativeLayout>
Как мне добиться этого влияет?