gpt4 book ai didi

android - 具有中心图像缩放功能的类似画廊的 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:39 25 4
gpt4 key购买 nike

这里我需要一个像画廊一样的 View ,一次只能在屏幕上显示三张图片。在此中间图像将大于其两侧的其他两个图像。

如果用户 ScrollView ,下一张图像将像在画廊中一样在屏幕上滑动,一次只会显示三张图像,其中中心图像在屏幕上显示时应该自动缩放,其余两张应该是比它小。

这里我不能使用 gallery,因为它在 android 中已经过时了。

在 viewpager 的帮助下,我能够使用 this link 上的代码制作类似 View 的图库.它一次只在屏幕上显示三个图像,这符合我的一个要求。但我无法获得屏幕上可见的中央图像并对其进行缩放。尽管我能够在屏幕上显示点击的图像。

谁能告诉我哪里需要修改this代码以及我需要在其中添加的内容,以便从屏幕上显示的图像中获取居中的图像并对其进行缩放。

我知道根据 viewpager,屏幕上没有中心图像,并且由于代码修改,它一次只在屏幕上显示三个图像。

我也试过:-

  • 水平滚动的 GridView
  • 具有水平线性布局的 Horizo​​ntalScrollView

但 viewpager 似乎是一个更好的解决方案,因为由于 viewpager 的固有属性,它在屏幕上只有三个项目时停止滚动。

如果有人知道任何其他实现它的方法,请告诉我,我会尝试。

P.S. 对于任何想要完整代码的人,我已将其添加为答案,它也具有缩放功能。 仅在已接受的答案中添加了一些内容。 :)

最佳答案

以下代码将帮助您制作一个像画廊一样的 View ,它将有中心锁定。它响应触摸和滑动。它一次在屏幕上显示三张图片,中间的图片被放大。

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

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;

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

/**
* Scrolls the list view to the currently active child.
*/
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);

ImageView centerImage = (ImageView)targetView;
int height=300;//set size of centered image
LinearLayout.LayoutParams flparams = new LinearLayout.LayoutParams(height, height);
centerImage.setLayoutParams(flparams);

//get the image to left of the centered image
if((targetItem-1)>=0){
targetLeft = getLinearLayout().getChildAt(targetItem-1);
leftImage = (ImageView)targetLeft;
int width=250;//set the size of left image
LinearLayout.LayoutParams leftParams = new LinearLayout.LayoutParams(width,width);
leftParams.setMargins(0, 30, 0, 0);
leftImage.setLayoutParams(leftParams);
}

//get the image to right of the centered image
if((targetItem+1)<maxItemCount){
targetRight = getLinearLayout().getChildAt(targetItem+1);
rightImage = (ImageView)targetRight;
int width=250;//set the size of right image
LinearLayout.LayoutParams rightParams = new LinearLayout.LayoutParams(width,width);
rightParams.setMargins(0, 30, 0, 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();
}

}

在您的 xml 中添加水平 ScrollView ,如下所示:-

<com.yourpackagename.CenteringHorizontalScrollView
android:id="@+id/HSVImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Horizontalalternative">

<LinearLayout
android:id="@+id/linearImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</com.yourpackagename.CenteringHorizontalScrollView>

在您的 Activity 中定义线性布局。

LinearLayout imageGallery;

然后获取如下:-

imageGallery=(LinearLayout)findViewById(R.id.linearImage);

现在您必须将 imageView 添加到您的 LinearLayout。在这里,我假设您的可绘制文件夹中有图像,并且您已经制作了一组要添加到图库的图像的 id。所以你可以在你的 Activity 中通过以下方法来做到这一点:-

for(int i=0; i<lengthOfImageIdArray; i++){
ImageView image=new ImageView(YourActivityName.this);
image.setBackgroundResource(yourArrayName[i]);
imageGallery.addView(image);
}

您还可以动态设置图像的宽度,以便它们适合每个屏幕,只需很少的额外努力。

关于android - 具有中心图像缩放功能的类似画廊的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18889131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com