Android - 在gridView Fragment的适配器中放大动画

时间:2015-04-08 07:28:18

标签: android android-fragments android-animation android-gridview

我尝试在gridView中的照片库中实现简单动画 - 放大照片。我有图库和适配器的片段,它控制片段中发生的所有事情。 问题是代码在片段中不起作用,我使用教程,但所有这些都在MainActivity中工作,我需要在我的适配器中。 没有错误,只有:

4753-4753 / com.apps.madzia.photo_application D / AbsListView:获取MotionRecognitionManager

之前在Logcat中有信息abaiut Skipped Frames但现在甚至没有显示。我的适配器: 公共类AdapterGallery扩展ArrayAdapter实现AdapterView.OnItemClickListener {

private ImageView expandedImageView;
private LayoutInflater inflater;
public Animator mCurrentAnimator;
public int mShortAnimationDuration;

public AdapterGallery(Context context, List<GalleryItem> objects) {
    super(context, R.layout.item_gallery, objects);
    inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItem = convertView;
    ViewHolder holder;

    if(listItem == null)
    {
        listItem= inflater.inflate(R.layout.item_gallery,parent,false);
        holder = new ViewHolder();
        holder.picture = (ImageView) listItem.findViewById(R.id.gallery_item);
        listItem.setTag(holder);
    }else{
     holder = (ViewHolder) listItem.getTag();
    }
    String plik = MainActivity.IMAGE_FILE_PATH+getItem(position).getPic_name()+".jpg";
    Log.i("test", plik);
    Picasso.with(getContext())
            .load(new File(plik))
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.picture);
    return listItem;

}

private class ViewHolder{
    ImageView picture;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   view = inflater.inflate(R.layout.fragment_gallery,parent,false);

}

public void zoomImageFromThumb(final View thumbView, int imageResId)
{
    if (mCurrentAnimator != null) {
        mCurrentAnimator.cancel();
    }

    final ImageView expandedImageView = (ImageView) thumbView.findViewById(
            R.id.expanded_image);
    String plik = MainActivity.IMAGE_FILE_PATH+getItem(imageResId).getPic_name()+".jpg";
    Picasso.with(getContext())
            .load(new File(plik))
            .placeholder(R.mipmap.ic_launcher)
            .into(expandedImageView);

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


    thumbView.getGlobalVisibleRect(startBounds);
    thumbView.findViewById(R.id.grid_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()) {

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


}

}

我使用适配器的片段:

查看forGallery = inflater.inflate(R.layout.fragment_gallery,container,false);

    GridView gridView = (GridView) forGallery.findViewById(R.id.gridViewGallery);
    AdapterGallery adapterGallery = new AdapterGallery(getActivity(),galleryList);
    gridView.setAdapter(adapterGallery);
    gridView.setOnItemClickListener(adapterGallery);

    // Retrieve and cache the system's default "short" animation time.
    adapterGallery.mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
    return forGallery;

}

0 个答案:

没有答案