PhotoView的使用--点击图片放大

今天的认识了一个开源代码PhotoView,主要就是实现图片点击放大的功能,那么这个玩意儿怎么使用呢, 第一步1.Gradle添加依赖 (推荐)

dependencies { compile 'com.bm.photoview:library:1.4.0' } 或者也可以将项目下载下来,将Info.Java和PhotoView.java两个文件拷贝到你的项目中,(不推荐) 第二步在xml中添加:

    

    


第三步在java代码中的引用:

public class MainActivity extends AppCompatActivity { View mBg; PhotoView mPhotoView; Info mInfo; View mParent; AlphaAnimation in = new AlphaAnimation(0, 1); AlphaAnimation out = new AlphaAnimation(1, 0); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); in.setDuration(300); out.setDuration(300); out.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mBg.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    mParent = findViewById(R.id.parent);
    mBg = findViewById(R.id.bg);
    mPhotoView = (PhotoView) findViewById(R.id.img);
    mPhotoView.setScaleType(ImageView.ScaleType.FIT_START);
    mPhotoView.enable();
    mPhotoView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBg.startAnimation(out);
            mPhotoView.animaTo(mInfo, new Runnable() {
                @Override
                public void run() {
                    mParent.setVisibility(View.GONE);
                }
            });
        }
    });
}
public void onClick(View view){
    PhotoView p = (PhotoView) findViewById(R.id.img);
    mInfo = p.getInfo();

    mPhotoView.setImageResource(R.mipmap.ic_launcher);
    mBg.startAnimation(in);
    mBg.setVisibility(View.VISIBLE);
    mParent.setVisibility(View.VISIBLE);;
    mPhotoView.animaFrom(mInfo);
}
@Override
public void onBackPressed() {
    if (mParent.getVisibility() == View.VISIBLE) {
        mBg.startAnimation(out);
        mPhotoView.animaTo(mInfo, new Runnable() {
            @Override
            public void run() {
                mParent.setVisibility(View.GONE);
            }
        });
    } else {
        super.onBackPressed();
    }
}

转载于:https://my.oschina.net/FrancisBingo/blog/685895

你可能感兴趣的:(PhotoView的使用--点击图片放大)