diff --git a/.idea/misc.xml b/.idea/misc.xml index 13c4629..ac59544 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,11 +5,12 @@ @@ -24,26 +25,10 @@ - + - - - - - 1.8 - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index bddf8b7..89fb324 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,10 +2,9 @@ - - + - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 7eb7184..2cd5443 100755 --- a/build.gradle +++ b/build.gradle @@ -6,13 +6,14 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files - classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2' + classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' } + } allprojects { diff --git a/example/build.gradle b/example/build.gradle index 9830b30..b5c43c4 100755 --- a/example/build.gradle +++ b/example/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 26 + compileSdkVersion 28 buildToolsVersion "26.0.2" defaultConfig { applicationId "com.example.rushd.galleryproject" minSdkVersion 16 - targetSdkVersion 26 + targetSdkVersion 28 versionCode 1 versionName "1.0" } @@ -22,8 +22,9 @@ android { dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:26.1.0' - compile 'com.android.support:recyclerview-v7:26.1.0' - compile 'com.squareup.picasso:picasso:2.5.0' - implementation project(':scrollingImageView') + implementation 'com.facebook.fresco:fresco:1.11.0' + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'com.android.support:recyclerview-v7:28.0.0' + implementation 'com.facebook.fresco:fresco:1.11.0' + api project(':scrollingImageView') } diff --git a/example/src/main/AndroidManifest.xml b/example/src/main/AndroidManifest.xml index c9559ad..8e0d662 100755 --- a/example/src/main/AndroidManifest.xml +++ b/example/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.example.rushd.galleryproject"> createLists = prepareData(); MyAdapter adapter = new MyAdapter(getApplicationContext(), createLists); recyclerView.setAdapter(adapter); } - private ArrayList prepareData(){ + + private ArrayList prepareData() { ArrayList images = new ArrayList<>(); - for(int i = 0; i< image_titles.length; i++){ - CreateList createList = new CreateList(); - createList.setImage_title(image_titles[i]); - createList.setImage_ID(image_ids[i]); - images.add(createList); + for (int j = 0; j < 5; j++) { + for (int i = 0; i < image_titles.length; i++) { + CreateList createList = new CreateList(); + createList.setImage_title(image_titles[i]); + createList.setImage_ID(image_ids[i]); + images.add(createList); + } } return images; } diff --git a/example/src/main/java/com/example/rushd/galleryproject/MyAdapter.java b/example/src/main/java/com/example/rushd/galleryproject/MyAdapter.java index 9c94210..fea57fc 100755 --- a/example/src/main/java/com/example/rushd/galleryproject/MyAdapter.java +++ b/example/src/main/java/com/example/rushd/galleryproject/MyAdapter.java @@ -10,6 +10,8 @@ import android.widget.ImageView; import android.widget.Toast; +import com.facebook.drawee.view.SimpleDraweeView; + import java.util.ArrayList; public class MyAdapter extends RecyclerView.Adapter { @@ -24,10 +26,10 @@ public MyAdapter(Context context, ArrayList galleryList) { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { - case 0 : + case 0: View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_layout, viewGroup, false); return new ViewHolder(view); - case 1 : + case 1: View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.scroll_img, viewGroup, false); return new ViewHolder2(view2); } @@ -37,7 +39,7 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewT @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (getItemViewType(position) == 1) { - ((ViewHolder2) holder).img.setImageResource((galleryList.get(position).getImage_ID())); + ((ViewHolder2) holder).img.setActualImageResource((galleryList.get(position).getImage_ID())); } else { ((ViewHolder) holder).img.setScaleType(ImageView.ScaleType.CENTER_CROP); ((ViewHolder) holder).img.setImageResource((galleryList.get(position).getImage_ID())); @@ -55,7 +57,7 @@ public void onClick(View v) { public int getItemViewType(int position) { // Just as an example, return 0 or 2 depending on position // Note that unlike in ListView adapters, types don't have to be contiguous - return position == 5 ? 1 : 0; + return position % 5 == 0 ? 1 : 0; } @@ -64,16 +66,18 @@ public int getItemCount() { return galleryList.size(); } - public class ViewHolder extends RecyclerView.ViewHolder{ + public class ViewHolder extends RecyclerView.ViewHolder { private ImageView img; + public ViewHolder(View view) { super(view); img = view.findViewById(R.id.img); } } - public class ViewHolder2 extends RecyclerView.ViewHolder{ - private ImageView img; + public class ViewHolder2 extends RecyclerView.ViewHolder { + private SimpleDraweeView img; + public ViewHolder2(View view) { super(view); img = view.findViewById(R.id.img); diff --git a/example/src/main/java/com/example/rushd/galleryproject/MyApplication.java b/example/src/main/java/com/example/rushd/galleryproject/MyApplication.java new file mode 100644 index 0000000..8819052 --- /dev/null +++ b/example/src/main/java/com/example/rushd/galleryproject/MyApplication.java @@ -0,0 +1,13 @@ +package com.example.rushd.galleryproject; + +import android.app.Application; + +import com.facebook.drawee.backends.pipeline.Fresco; + +public class MyApplication extends Application { + @Override + public void onCreate() { + super.onCreate(); + Fresco.initialize(this); + } +} diff --git a/example/src/main/res/layout/scroll_img.xml b/example/src/main/res/layout/scroll_img.xml index f4ca429..ffa7c73 100755 --- a/example/src/main/res/layout/scroll_img.xml +++ b/example/src/main/res/layout/scroll_img.xml @@ -1,14 +1,14 @@ - - + + - + android:layout_height="600dp" + custom:actualImageResource="@mipmap/img6" /> + diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7e97c97..65da79e 100755 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/scrollingImageView/build.gradle b/scrollingImageView/build.gradle old mode 100644 new mode 100755 index b90696d..39cf41b --- a/scrollingImageView/build.gradle +++ b/scrollingImageView/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 26 + compileSdkVersion 28 defaultConfig { minSdkVersion 16 - targetSdkVersion 26 + targetSdkVersion 28 versionCode 1 versionName "1.0" @@ -24,11 +24,10 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - - implementation 'com.android.support:appcompat-v7:26.1.0' - implementation 'com.android.support:recyclerview-v7:26.1.0' + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'com.android.support:recyclerview-v7:28.0.0' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.1' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' + androidTestImplementation 'com.android.support.test:runner:1.0.2' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } apply from: './bintray.gradle' diff --git a/scrollingImageView/src/main/java/com/yel/image/ScrollingImageView.java b/scrollingImageView/src/main/java/com/yel/image/ScrollingImageView.java old mode 100644 new mode 100755 index 0e63e1a..1adbb9a --- a/scrollingImageView/src/main/java/com/yel/image/ScrollingImageView.java +++ b/scrollingImageView/src/main/java/com/yel/image/ScrollingImageView.java @@ -5,30 +5,35 @@ import android.content.Context; import android.content.res.TypedArray; +import android.graphics.Canvas; import android.graphics.Matrix; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; +import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.widget.FrameLayout; -public class ScrollingImageView extends android.support.v7.widget.AppCompatImageView { - private float yPercent = 0.5f; +public class ScrollingImageView extends FrameLayout { private ScrollType mScrollType; + private View child; + public enum ScrollType { /** * View从开始到结束,一直在滑动 */ - SCROLL_WHOLE (0), + SCROLL_WHOLE(0), /** * View只当在完整显示时才开始滚动 */ - SCROLL_MIDDLE (1); + SCROLL_MIDDLE(1); ScrollType(int ni) { nativeInt = ni; } + final int nativeInt; } @@ -39,7 +44,6 @@ public enum ScrollType { public ScrollingImageView(Context context) { super(context); - setup(); } public ScrollingImageView(Context context, AttributeSet attrs) { @@ -61,10 +65,16 @@ public ScrollingImageView(Context context, AttributeSet attrs, a.recycle(); } - RecyclerView temp = getRecyclerView((ViewGroup) this.getParent()); - System.out.println("temp: " + temp); + } - setup(); + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + if (child == null) { + RecyclerView temp = getRecyclerView((ViewGroup) this.getParent()); + temp.addOnScrollListener(new ScrollListener(this)); + child = getChildAt(0); + } } private RecyclerView getRecyclerView(ViewGroup v) { @@ -76,15 +86,11 @@ private RecyclerView getRecyclerView(ViewGroup v) { } return getRecyclerView((ViewGroup) v.getParent()); } - private void setup() { - // 设置ImageView的必须ScaleType - setScaleType(ScaleType.MATRIX); - } + public void setScrollType(ScrollType scrollType) { if (mScrollType != scrollType) { mScrollType = scrollType; - // todo 好像要做点什么。。 } } @@ -92,31 +98,15 @@ public ScrollType getScrollType() { return mScrollType; } - public void setyPercent(float yPercent) { - this.yPercent = yPercent; - requestLayout(); - // 由于在setFrame中的setImageMatrix中调用了invalidate。所以这里不必再调用了 - // invalidate(); - } - - private int getViewHeight() { - return getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); - } - - @Override - protected boolean setFrame(int l, int t, int r, int b) { - if (getDrawable() == null) - return super.setFrame(l, t, r, b); - - Matrix matrix = getImageMatrix(); - + public void setPercent(float yPercent) { + matrix = child.getMatrix(); float scale; float dx = 0, dy = 0; int viewWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); int viewHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); - int drawableWidth = getDrawable().getIntrinsicWidth(); - int drawableHeight = getDrawable().getIntrinsicHeight(); + int drawableWidth = child.getMeasuredWidth(); + int drawableHeight = child.getMeasuredHeight(); // Get the scale if (drawableWidth * viewHeight > drawableHeight * viewWidth) { scale = (float) viewHeight / (float) drawableHeight; @@ -131,54 +121,63 @@ protected boolean setFrame(int l, int t, int r, int b) { dy = (viewHeight - drawableHeight * scale) * yPercent; } } - + Log.i("ScrollListener", "dy:" + dy); matrix.setScale(scale, scale); matrix.postTranslate(Math.round(dx), Math.round(dy)); + invalidate(); + } + + private int getViewHeight() { + return getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); + } - setImageMatrix(matrix); + Matrix matrix; - return super.setFrame(l, t, r, b); + + @Override + protected void dispatchDraw(Canvas canvas) { + canvas.save(); + canvas.concat(matrix); + super.dispatchDraw(canvas); + canvas.restore(); } - public static class ScrollListener extends RecyclerView.OnScrollListener { + + private class ScrollListener extends RecyclerView.OnScrollListener { int ydy = 0; - private RecyclerView.LayoutManager layoutManager; - private int id; - private int index; - public ScrollListener(RecyclerView.LayoutManager layoutManager, int id, int index) { - this.layoutManager = layoutManager; - this.id = id; - this.index = index; + private ScrollingImageView child; + + ScrollListener(ScrollingImageView child) { + this.child = child; } + @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); ydy += dy; - System.out.println(ydy); - - View item = layoutManager.findViewByPosition(index); - if (item != null) { - float Offset = item.getTop(); - ScrollingImageView image = item.findViewById(id); - int viewHeight = image.getViewHeight(); + if (child != null) { + float Offset = child.getTop(); + int viewHeight = child.getViewHeight(); int recyclerViewHeight = recyclerView.getHeight(); - -// System.out.println("Height: " + recyclerViewHeight); -// System.out.println("Offset: " + Offset); - final double bottomDelta = recyclerViewHeight - viewHeight; + // Log.i("ScrollListener", "offset:" + Offset); + // Log.i("ScrollListener", "bottomDelta:" + bottomDelta); + - if (image.getScrollType() == ScrollType.SCROLL_WHOLE) { + if (child.getScrollType() == ScrollType.SCROLL_WHOLE) { float ratio = (Offset + viewHeight) / 1.0f / (recyclerViewHeight + viewHeight); - image.setyPercent(ratio); - } else if (image.getScrollType() == ScrollType.SCROLL_MIDDLE) { + + child.setPercent(ratio); + } else if (child.getScrollType() == ScrollType.SCROLL_MIDDLE) { + float percent = (float) (Offset / bottomDelta); + // Log.i("ScrollListener", "percent:" + percent); if (Offset > bottomDelta) { - image.setyPercent(1); + child.setPercent(1); } else if (Offset <= 0) { - image.setyPercent(0); + child.setPercent(0); } else { - image.setyPercent((float) (Offset / bottomDelta)); + child.setPercent(percent); } } }