如何在android中检查是否和image设置为imageview

时间:2016-05-04 19:59:21

标签: android bitmap imageview android-progressbar recycle

我正在尝试检查图像(位图)是否设置为imageview,如果是,我将停止进度条可见性。

我的代码如下:

我打电话给服务器获取图像,内部监听器成功我按如下方式设置了imageview:

 mImageView.setImageBitmap(bitmap);
 if(hasImage(mImageView)) {
    showProgress(false);
 }

public void showProgress(final boolean show) {
    int shortAnimTime =    getResources().
                            getInteger(android.R.integer.
                                            config_shortAnimTime);

    mImageView.setVisibility(show ? View.GONE : View.VISIBLE);
    mImageView.animate().setDuration(shortAnimTime).alpha(
            show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mImageView.setVisibility(show ? View.GONE : View.VISIBLE);
        }
    });

    mProgressBar.setVisibility(show ? View.VISIBLE : View.GONE);
    mProgressBar.animate().setDuration(shortAnimTime).alpha(
            show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mProgressBar.setVisibility(show ? View.VISIBLE:View.GONE);
        }
    });
}

private boolean hasImage(ImageView view) {
    Drawable drawable = view.getDrawable();
    boolean hasImage = (drawable != null);

    if (hasImage && (drawable instanceof BitmapDrawable)) {
        hasImage = ((BitmapDrawable)drawable).getBitmap() != null;
    }       

    return hasImage;
}

图像显示正常但我的应用程序崩溃时,我滑动图像太快,出现以下错误:

以下是两种错误:

logcat 1:

FATAL EXCEPTION: main
                                                                Process: com.ceiva.snap, PID: 28985
                                                                java.lang.IllegalStateException: Fragment PhotoFragment{1c7b662a} not attached to Activity
                                                                    at android.support.v4.app.Fragment.getResources(Fragment.java:620)
                                                                    at com.ceiva.snap.PhotoFragment.showProgress(PhotoFragment.java:196)
                                                                    at com.ceiva.snap.PhotoFragment$1.pictureRequestSuccessful(PhotoFragment.java:180)
                                                                    at com.ceiva.snap.cws.CEIVAWebServices$22.onResponse(CEIVAWebServices.java:829)
                                                                    at com.ceiva.snap.cws.CEIVAWebServices$22.onResponse(CEIVAWebServices.java:820)
                                                                    at com.ceiva.snap.cws.CEIVAImageRequest.deliverResponse(CEIVAImageRequest.java:235)
                                                                    at com.ceiva.snap.cws.CEIVAImageRequest.deliverResponse(CEIVAImageRequest.java:52)
                                                                    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:145)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

Log cat2:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@9e161c3
                                                                    at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1225)
                                                                    at android.graphics.Canvas.drawBitmap(Canvas.java:1312)
                                                                    at android.graphics.Bitmap.createBitmap(Bitmap.java:863)
                                                                    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:719)
                                                                    at com.ceiva.snap.DeviceImageAdapter.getView(DeviceImageAdapter.java:302)
                                                                    at android.widget.AbsListView.obtainView(AbsListView.java:2823)
                                                                    at android.widget.GridView.makeAndAddView(GridView.java:1437)
                                                                    at android.widget.GridView.makeRow(GridView.java:361)
                                                                    at android.widget.GridView.fillDown(GridView.java:302)
                                                                    at android.widget.GridView.fillSpecific(GridView.java:586)
                                                                    at android.widget.GridView.layoutChildren(GridView.java:1291)
                                                                    at android.widget.AbsListView.onLayout(AbsListView.java:2627)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
                                                                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
                                                                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                    at android.view.View.layout(View.java:16711)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5328)
                                                                    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2319)
                                                                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2032)
                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
                                                                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:590)
                                                                    at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:145)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

0 个答案:

没有答案