photoView 이미지 멀 티 터치 효과 구현
효과:클릭 하면 자 유 롭 게 크기 조정,이동 할 수 있 습 니 다.
의존 도 추가:
compile 'com.commit451:PhotoView:1.2.4'
compile 'com.github.bumptech.glide:glide:3.5.2'
레이아웃 파일:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<!-- Viewpager -->
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll_point"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/viewPager"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<com.example.tuhuadmin.imagetouch.HackyViewPager
android:id="@+id/hViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
코드 구현:
package com.example.tuhuadmin.imagetouch;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private LinearLayout ll_point;
private ArrayList<String> mImages;
// ImageView
private ArrayList<ImageView> imageViewsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initImageUrl();
getImageData();
}
private void initImageUrl() {
//
mImages = new ArrayList<String>();
mImages.add(Constant.url1);
mImages.add(Constant.url2);
mImages.add(Constant.url4);
mImages.add(Constant.url5);
mImages.add(Constant.url6);
imageViewsList = new ArrayList<>();
}
private void initViews() {
vp = (ViewPager) findViewById(R.id.viewPager);
ll_point = (LinearLayout) findViewById(R.id.ll_point);
//
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, ScreenUtils.getScreenWidth(this));
vp.setLayoutParams(params);
}
public void getImageData() {
if (mImages.size() == 1) {
//
} else {
for (int i = 0; i < mImages.size(); i++) {
View point = new View(this);
//point.setId(i);// Id
point.setTag(i);// Tag
//
point.setBackgroundResource(R.drawable.point_selector);
//
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
params.rightMargin = 20;//
//
ll_point.addView(point, params);
}
View point = ll_point.getChildAt(0);
point.setEnabled(false);
}
for (int i = 0; i < mImages.size(); i++) {
ImageView iv = new ImageView(this);
// iv
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.getScreenWidth(this));
iv.setLayoutParams(params);
// iv --->
iv.setScaleType(ImageView.ScaleType.FIT_XY);
String url = mImages.get(i);
Glide.with(this).load(url).into(iv);
//
// ImageView 、
iv.setTag(i);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = (int) view.getTag();
Intent intent = new Intent(MainActivity.this, PhotoShowActivity.class);
// 、
intent.putExtra("position", position);
intent.putStringArrayListExtra("mImages", mImages);
startActivity(intent);
}
});
//
imageViewsList.add(iv);
}
vp.setAdapter(new ViewPagerAdapter(imageViewsList,vp) );
vp.setOnPageChangeListener(new MyOnPageChangeListener());
}
private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
//
@Override
public void onPageSelected(int index) {
changePager(index);
}
}
private void changePager(int index) {
int childCount = ll_point.getChildCount();
for (int i = 0; i < childCount; i++) {
View point = ll_point.getChildAt(i);//
if (index == i) {
//
point.setEnabled(false);
} else {
point.setEnabled(true);
}
}
}
}
PhotoShowActivity 에서 코드 구현:
package com.example.tuhuadmin.imagetouch;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
/**
* Created on 2016/7/19.
* Author:crs
* Description:XXX
*/
public class PhotoShowActivity extends Activity {
private int position;
private ArrayList<String> mDatas;
private HackyViewPager hViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_show);
getFrontPageData();
initViews();
}
private void initViews() {
hViewPager = (HackyViewPager) findViewById(R.id.hViewPager);
hViewPager.setAdapter(new ImageAdapter());
// ViewPager page
hViewPager.setCurrentItem(position);
}
public void getFrontPageData() {
//
position = getIntent().getIntExtra("position", 0);
//
mDatas = getIntent().getStringArrayListExtra("mImages");
}
public class ImageAdapter extends PagerAdapter {
@Override
public int getCount() {
return null == mDatas ? 0 : mDatas.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//
PhotoView photoView = new PhotoView(container.getContext());
//
photoView.setBackgroundColor(Color.WHITE);
// photoView viewpager ,
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// PhtotoView
String url = mDatas.get(position);
if (!TextUtils.isEmpty(url)) {
// Glide
Glide.with(PhotoShowActivity.this).load(url).into(photoView);
}
//
photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
@Override
public void onViewTap(View view, float x, float y) {
finish();
}
});
return photoView;
}
}
}
ScreenUtils 의 코드:WindowManager 클래스 사용 WINDOW_SERVICE 디 스 플레이(화면 매개 변수 도구 클래스)
package com.example.tuhuadmin.imagetouch;
import android.content.Context;
import android.view.Display;
import android.view.WindowManager;
/**
* Created on 2016/7/19.
* Author:crs
* Description:XXX
*/
public class ScreenUtils {
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display defaultDisplay = wm.getDefaultDisplay();
int width = defaultDisplay.getWidth();
return width;
}
}
Constant 인터페이스 에 저 장 된 문자열 상수:
package com.example.tuhuadmin.imagetouch;
/**
* Created on 2016/7/19.
* Author:crs
* Description:XXX
*/
public interface Constant {
String url1 = "http://pic.yesky.com/uploadImages/2016/181/06/L3JL486FYCL7.jpg";
String url2 = "http://pic.yesky.com/uploadImages/2016/181/07/9MXS11F0HOC2.jpg";
String url5 = "http://xm.fjsen.com/images/attachement/jpg/site2/20160629/38c986e7161c18dd5a6c1d.jpg";
String url4 = "http://pimg.damai.cn/perform/project/281/28183_n.jpg";
String url6 = "http://i3.hunantv.com/p1/20110905/1702374043.jpg";
}
ViewPagerAdapter:
package com.example.tuhuadmin.imagetouch;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created on 2016/7/19.
* Author:crs
* Description:XXX
*/
public class ViewPagerAdapter extends PagerAdapter {
ArrayList<ImageView> mImages;
ViewPager vp;
public ViewPagerAdapter(ArrayList<ImageView> mImages, ViewPager vp) {
this.mImages = mImages;
this.vp = vp;
}
@Override
public int getCount() {
return null == mImages ? 0 : mImages.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ImageView iv = mImages.get(position);
vp.removeView(iv);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv = mImages.get(position);
vp.addView(iv);
return mImages.get(position);
}
}
코드이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
GCD 로 그림 다운로드 하기 (2)텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.