您好,登錄后才能下訂單哦!
我想,不管是做什么樣的app。一般都會涉及到圖片點擊方法,和左右滑動的功能吧。今天我就把自己的實現拿來分享一下。圖片的加載用到了picasso的圖片加載工具。
第一步:注入依賴
compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.github.chrisbanes:PhotoView:1.3.0'
第二步,我們就來實現這個功能。多張圖片左右滑動,這需要用到ViewPager這個類。
且看布局:
<uk.co.senab.photoview.sample.HackyViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/iv_photo" android:layout_width="match_parent" android:layout_height="match_parent"/>
這里用到了自定義的ViewPager
public class HackyViewPager extends ViewPager { public HackyViewPager(Context context) { super(context); } public HackyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { try { return super.onInterceptTouchEvent(ev); } catch (IllegalArgumentException e) { e.printStackTrace(); return false; } } }
這個是點擊之后跳轉到大圖的Activity,來顯示大圖。最重要也是很簡單的一步:
package uk.co.senab.photoview.sample; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import uk.co.senab.photoview.PhotoView; import uk.co.senab.photoview.PhotoViewAttacher; public class PicassoSampleActivity extends AppCompatActivity { ViewPager mViewPager; //String position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple); mViewPager = (HackyViewPager) findViewById(R.id.iv_photo); setContentView(mViewPager); mViewPager.setAdapter(new SamplePagerAdapter()); //position=getIntent().getStringExtra("position"); //設置默認的坐標,實際情況不要寫死,只需點擊哪張圖片的position,并傳過來接受就可以了,就默認顯示點擊圖片的大圖。 //mViewPager.setCurrentItem(Integer.parseInt(position)); mViewPager.setCurrentItem(2); } static class SamplePagerAdapter extends PagerAdapter { //這里暫時寫死了,實際情況中要從服務端獲取圖片地址結合,傳過來 private static final String[] url = {"/media/Bist9mvIYAAeAyQ.jpg", "/album/whcrop%3D657%2C370%3Bq%3D90/sign=2c994e578a82b9013df895711cfd9441/09fa513d269759eede0805bbb2fb43166d22df62.jpg", "/d/file/2013-11-14/13631d143b7076dc3257b1e516a2a9a2.jpg", "/f5/63/d/7.jpg"}; @Override public int getCount() { return url.length; } @Override public View instantiateItem(ViewGroup container, int position) { PhotoView photoView = new PhotoView(container.getContext()); final PhotoViewAttacher attacher = new PhotoViewAttacher(photoView); Picasso.with(container.getContext()) .load(url[position]) .into(photoView, new Callback() { @Override public void onSuccess() { attacher.update(); } @Override public void onError() { } }); container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); return photoView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } }
有了這個工具我們很輕松就實現了圖片點擊放大,左右活動,縮放等功能。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。