【发布时间】:2015-04-06 18:14:40
【问题描述】:
我有一个线性布局,其中有两个 imageview 第一个设置为显示正常图像,另一个是隐藏,当任何用户触摸第一个 imageview 第二个图像视图可见并设置为全屏时..但在此期间我得到了错误哪个我发给你... 我有followed this code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/liimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/property_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="matrix"
android:src="@drawable/house" />
</LinearLayout>
<ImageView
android:id="@+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
我的代码
property_image.setImageBitmap(bmp);
property_image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoomImageFromPhoto(property_image, property_image);
}
});
propertyImage 是我的第一张图片,它显示我的图片,但是当我触摸它时,它会转到 zoomImagefromphoto 类
private void zoomImageFromPhoto(final View thumbView, ImageView property_image2) {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}
final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
// expandedImageView.setImageResource(property_image2);
final Rect startBounds = new Rect();
final Rect finalBounds = new Rect();
final Point globalOffset = new Point();
thumbView.getGlobalVisibleRect(startBounds);
findViewById(R.id.liimage).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()) {
startScale = (float) startBounds.height() / finalBounds.height();
float startWidth = startScale * finalBounds.width();
float deltaWidth = (startWidth - startBounds.width()) / 2;
startBounds.left -= deltaWidth;
startBounds.right += deltaWidth;
} else {
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;
}
});
}
但我遇到了类似的错误
04-06 11:02:31.649: E/AndroidRuntime(2739): Process: com.big_property, PID: 2739
04-06 11:02:31.649: E/AndroidRuntime(2739): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.View.getGlobalVisibleRect(android.graphics.Rect, android.graphics.Point)' on a null object reference
04-06 11:02:31.649: E/AndroidRuntime(2739):at com.big_property.search_property_activity.zoomImageFromPhoto(search_property_ activity.java:284)
04-06 11:02:31.649: E/AndroidRuntime(2739):at com.big_property.search_property_activity.access$4(search_property_activity.java:274)
04-06 11:02:31.649: E/AndroidRuntime(2739):at com.big_property.search_property_activity$3.onClick(search_property_activity.java:268)
我认为 Eror 在这一行
findViewById(R.id.liimage).getGlobalVisibleRect(finalBounds, globalOffset);
如何解决这个问题?帮助我的朋友提前谢谢。
【问题讨论】:
-
Help Me Frienif 任何人都知道如何解决这个问题,然后我才知道是否有人知道它找不到我的线性布局 id....
-
如果我有误解请纠正我。我认为当用户单击带有 alpha 动画的同一屏幕中的小图标图像时,您想显示一个大图像?我正确吗?
-
写@Srinivasan 那么怎么做
标签: android android-layout android-activity android-animation android-imageview