将Imageview设置为活动全屏

时间:2015-04-06 05:50:29

标签: android android-layout android-activity android-animation android-imageview

我有一个线性布局,有两个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);

如何解决这个问题?请事先帮助我朋友。

4 个答案:

答案 0 :(得分:2)

取代zoomImageFromPhoto(最终查看thumbView,ImageView property_image2) 使用zoomImageFromPhoto(最终查看thumbView,int property_image2) 之后不要忘记这样做 expandedImageView.setImageResource(property_image2);

应该做出的改变如下......希望这会有所帮助

public class MainActivity extends Activity implements OnClickListener {

ImageView property_image;
private Animator mCurrentAnimator;
private int mShortAnimationDuration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    property_image=(ImageView) findViewById(R.id.property_image);
    Bitmap bmp=BitmapFactory.decodeResource(this.getResources(),
            R.drawable.desert);
    property_image.setImageBitmap(bmp);
    mShortAnimationDuration = getResources().getInteger(
            android.R.integer.config_shortAnimTime);

    property_image.setOnClickListener(this);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    zoomImageFromPhoto(property_image, R.drawable.desert);
}

 private void zoomImageFromPhoto(final View thumbView, int  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);
     expandedImageView.setImageResource(property_image2);
     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;
         }
     });
 }

}

答案 1 :(得分:0)

而不是这种方法,你可以这样做,当点击图像添加一个片段/活动,以全屏显示你的图像。

答案 2 :(得分:0)

请找到布局设计

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/img_small_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/ic_launcher"
        android:layout_centerInParent="true"/>

    <ImageView 
        android:id="@+id/img_large_icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_launcher"
        android:layout_centerInParent="true"
android:visibility="invisible"/>

为'img_small_icon'小图标添加点击监听器。 例如:

    ImageView imgSmall,imgLarge;
imgSmall = (ImageView) findViewById(R.id.img_small_icon);
        imgLarge = (ImageView) findViewById(R.id.img_large_icon);
        imgSmall.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(imgLarge.getVisibility() == View.INVISIBLE){
//Here you can add the Fade In/Out animation start
                    imgLarge.setVisibility(View.VISIBLE);

                }
            }
        });

希望这会对你有所帮助。如果你发现任何困难,请告诉我。

<强>更新

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha 
        android:duration="500"
        android:fromAlpha="5.0"
        android:toAlpha="5.0"/>

</set>

将此文件保存在drawable fodler中。

在你的onClickListener

    Animation fadeOutAnimation = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
imgLarge.startAnimation(fadeOutAnimation);

答案 3 :(得分:0)

没有必要做这么冗长的过程。只需要你需要一个xml类

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/img_large_icon"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后将该图像转换为字节数组

   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
   byteArray = stream.toByteArray();

单击pics时使用intent

发送此字节数组
intent.putExtra("Image", byteArray);

进行另一项活动

它将进入下一个活动

编写此代码

 public class Image_fullscreen_activity extends Activity {
  private Bitmap bmp;
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.fullscreen_image);
    byte[] byteArray = getIntent().getByteArrayExtra("Image");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    ImageView iv=(ImageView)findViewById(R.id.img_large_icon);
    iv.setImageBitmap(bmp);
    }   
@Override
public void onBackPressed() {
        super.onBackPressed();
        }

 }

在你的android mainfiest寄存器中

 <activity
        android:name=".Image_fullscreen_activity"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:label="@string/app_name" >
    </activity> 

只要这样做就可以获得准确的输出