Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

因为我的显示不出来,我修改了一下. #1

Open
aresjoy opened this issue Dec 8, 2017 · 0 comments
Open

因为我的显示不出来,我修改了一下. #1

aresjoy opened this issue Dec 8, 2017 · 0 comments

Comments

@aresjoy
Copy link

aresjoy commented Dec 8, 2017

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
public class ScrollImageView extends AppCompatImageView {
 //需要设置为 scaltype = matrix

 public ScrollImageView(Context context) {
        super(context);
    }

    public ScrollImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     加这个到addScrollListener
        if(contentListAdapter.getData().size()>0){
     int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
     for(int i=firstVisibleItemPosition;i<=lastVisibleItemPosition;i++){
     View view = layoutManager.findViewByPosition(i);
     ScrollImageView id_content_img = view.findViewById(R.id.id_content_img);
     if(id_content_img!=null){

     id_content_img.setDx(view.getTop(),layoutManager.getHeight());
     }

     }

     }
     */

    /**
      移动距离
     */
    private int mDx;

    // layout height ~0
    public void setDx(int viewTop, int layoutHeight){

        if(getDrawable() ==null){
            return;
        }

        int calculateHeight = getDrawable().getBounds().height() -viewHeight;
        if(calculateHeight<=0){
            //图片太小
            mDx =0;
            invalidate();
            return;
        }
        float rate = viewTop*1.0f/layoutHeight;
        mDx = (int) (calculateHeight*rate);
        invalidate();
    }

    private int viewHeight;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable =getDrawable();
        if(drawable==null){
            super.onDraw(canvas);
            return;
        }
        //设置图片大小根据View的宽度
        int w = getWidth();
        int h = (int)(getWidth()*1.0f*drawable.getIntrinsicHeight()/drawable.getIntrinsicWidth());
        drawable.setBounds(0,0,w,h);
        canvas.save();
        canvas.translate(0,-mDx);
        super.onDraw(canvas);
        canvas.restore();
    }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant