gpt4 book ai didi

android - GridView 缩放图像

转载 作者:行者123 更新时间:2023-11-29 00:29:13 27 4
gpt4 key购买 nike

我有一个 gridview,我正在尝试在 GridView Item Click 上缩放图像。使用来自谷歌的示例 http://developer.android.com/training/animation/zoom.html .我的问题是如何将 R.drawable 传递给“int imageResId”。在下面查看我的代码。

主 Activity .java

public class GridViewActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setTitle("GridView");
setContentView(R.layout.grid_view);


GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_grid_view, menu);
return true;
}
}

GridView .xml

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

ImageAdapter.java

 public class ImageAdapter extends BaseAdapter {

private Animator mCurrentAnimator;

private int mShortAnimationDuration;

private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return mThumbIds[position];
}

public long getItemId(int position) {
return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}

imageView.setImageResource(mThumbIds[position]);
imageView.setTag(mThumbIds[position]);
imageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
int id = (Integer) arg0.getTag();
zoomImageFromThumb(arg0, id);
}
});
return imageView;
}

// References to our images in res > drawable
public Integer[] mThumbIds = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_8, R.drawable.sample_9, R.drawable.sample_10,
R.drawable.sample_11, R.drawable.sample_12, R.drawable.sample_13,
R.drawable.sample_14, R.drawable.sample_15, R.drawable.sample_16,
R.drawable.sample_17, R.drawable.sample_18 };

private void zoomImageFromThumb(final View thumbView, int imageResId) {

if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}

final ImageView expandedImageView = (ImageView) thumbView.findViewById(R.id.expanded_image);
expandedImageView.setImageResource(imageResId);

final Rect startBounds = new Rect();
final Rect finalBounds = new Rect();
final Point globalOffset = new Point();

thumbView.getGlobalVisibleRect(startBounds);
thumbView.findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);
startBounds.offset(-globalOffset.x, -globalOffset.y);
finalBounds.offset(-globalOffset.x, -globalOffset.y);

float startScale;
if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds
.width() / startBounds.height()) {
// Extend start bounds horizontally
startScale = (float) startBounds.height() / finalBounds.height();
float startWidth = startScale * finalBounds.width();
float deltaWidth = (startWidth - startBounds.width()) / 2;
startBounds.left -= deltaWidth;
startBounds.right += deltaWidth;
} else {
// Extend start bounds vertically
startScale = (float) startBounds.width() / finalBounds.width();
float startHeight = startScale * finalBounds.height();
float deltaHeight = (startHeight - startBounds.height()) / 2;
startBounds.top -= deltaHeight;
startBounds.bottom += deltaHeight;
}

thumbView.setAlpha(0f);
expandedImageView.setVisibility(View.VISIBLE);

expandedImageView.setPivotX(0f);
expandedImageView.setPivotY(0f);

AnimatorSet set = new AnimatorSet();
set.play(
ObjectAnimator.ofFloat(expandedImageView, View.X,
startBounds.left, finalBounds.left))
.with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
startBounds.top, finalBounds.top))
.with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X,
startScale, 1f))
.with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y,
startScale, 1f));
set.setDuration(mShortAnimationDuration);
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mCurrentAnimator = null;
}

@Override
public void onAnimationCancel(Animator animation) {
mCurrentAnimator = null;
}
});
set.start();
mCurrentAnimator = set;

final float startScaleFinal = startScale;
expandedImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}

AnimatorSet set = new AnimatorSet();
set.play(
ObjectAnimator.ofFloat(expandedImageView, View.X,
startBounds.left))
.with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
startBounds.top))
.with(ObjectAnimator.ofFloat(expandedImageView,
View.SCALE_X, startScaleFinal))
.with(ObjectAnimator.ofFloat(expandedImageView,
View.SCALE_Y, startScaleFinal));
set.setDuration(mShortAnimationDuration);
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
thumbView.setAlpha(1f);
expandedImageView.setVisibility(View.GONE);
mCurrentAnimator = null;
}

@Override
public void onAnimationCancel(Animator animation) {
thumbView.setAlpha(1f);
expandedImageView.setVisibility(View.GONE);
mCurrentAnimator = null;
}
});
set.start();
mCurrentAnimator = set;
}
});
}
}

activity_zoom.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />

</FrameLayout>

洛卡特:

  06-14 13:58:47.667: E/AndroidRuntime(14860): FATAL EXCEPTION: main
06-14 13:58:47.667: E/AndroidRuntime(14860): java.lang.NullPointerException
06-14 13:58:47.667: E/AndroidRuntime(14860): at com.example.gridview.ImageAdapter.zoomImageFromThumb(ImageAdapter.java:79)
06-14 13:58:47.667: E/AndroidRuntime(14860): at com.example.gridview.ImageAdapter.access$0(ImageAdapter.java:72)
06-14 13:58:47.667: E/AndroidRuntime(14860): at com.example.gridview.ImageAdapter$1.onClick(ImageAdapter.java:57)
06-14 13:58:47.667: E/AndroidRuntime(14860): at android.view.View.performClick(View.java:3620)
06-14 13:58:47.667: E/AndroidRuntime(14860): at android.view.View$PerformClick.run(View.java:14292)
06-14 13:58:47.667: E/AndroidRuntime(14860): at android.os.Handler.handleCallback(Handler.java:605)
06-14 13:58:47.667: E/AndroidRuntime(14860): at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 13:58:47.667: E/AndroidRuntime(14860): at android.os.Looper.loop(Looper.java:137)
06-14 13:58:47.667: E/AndroidRuntime(14860): at android.app.ActivityThread.main(ActivityThread.java:4507)
06-14 13:58:47.667: E/AndroidRuntime(14860): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 13:58:47.667: E/AndroidRuntime(14860): at java.lang.reflect.Method.invoke(Method.java:511)
06-14 13:58:47.667: E/AndroidRuntime(14860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
06-14 13:58:47.667: E/AndroidRuntime(14860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
06-14 13:58:47.667: E/AndroidRuntime(14860): at dalvik.system.NativeStart.main(Native Method)

最佳答案

在示例中尝试这样做,您会在点击的 View 中找到展开的 View

final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
expandedImageView.setImageResource(imageResId);

zoomImageFromThumb(final View thumbView, int imageResId)

或者您可能需要在主 fragment View 中找到该 View ,如

final ImageView expandedImageView = (ImageView) mainView.findViewById(R.id.expanded_image);
expandedImageView.setImageResource(imageResId);

不在 thumbView 中。

编辑:

如下更改您的gridview.xml,将两个 View 放在相同的布局中

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

<ImageView
android:id="@+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />

您可以对两者使用相对布局包装器,以便一个与另一个重叠

并使用

final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
expandedImageView.setImageResource(imageResId);

zoomImageFromThumb(final View thumbView, int imageResId)

如果出现 symbol not find findViewById() 错误,则添加

public static Activity getActivity(){
归还这个;
GridViewActivity.java

然后使用

final ImageView expandedImageView = (ImageView) GridViewActivity.getActivity().findViewById(R.id.expanded_image);
expandedImageView.setImageResource(imageResId);

zoomImageFromThumb(final View thumbView, int imageResId)

关于android - GridView 缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16988844/

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