Skip to content

Commit

Permalink
1. 修复除数为0导致的异常;
Browse files Browse the repository at this point in the history
2. 增加 3D Item 展示效果;
3. 解决小数取整导致的位置计算错误,Item 位置错乱问题
  • Loading branch information
ChenLittlePing committed Sep 1, 2020
1 parent 36bb592 commit e63ac14
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 26 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package="com.recycler.coverflow">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -22,6 +24,7 @@
<activity android:name=".viewpager.ViewpagerActivity" />

<activity android:name=".recyclerview.RecyclerViewActivity" />
<activity android:name=".CoverFlow3DActivity" />
</application>

</manifest>
11 changes: 8 additions & 3 deletions app/src/main/java/com/recycler/coverflow/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
R.mipmap.item5,R.mipmap.item6};

private onItemClick clickCb;
private boolean is3D;

public Adapter(Context c) {
public Adapter(Context c, boolean is3D) {
mContext = c;
this.is3D = is3D;
}

public Adapter(Context c, onItemClick cb) {
public Adapter(Context c, onItemClick cb, boolean is3D) {
mContext = c;
clickCb = cb;
this.is3D = is3D;
}

public void setOnClickLstn(onItemClick cb) {
Expand All @@ -36,7 +39,9 @@ public void setOnClickLstn(onItemClick cb) {

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_item, parent, false);
int layout = R.layout.layout_item;
if (is3D) layout = R.layout.layout_item_mirror;
View v = LayoutInflater.from(mContext).inflate(layout, parent, false);
return new ViewHolder(v);
}

Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/com/recycler/coverflow/CoverFlow3DActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.recycler.coverflow;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import recycler.coverflow.CoverFlowLayoutManger;
import recycler.coverflow.RecyclerCoverFlow;

/**
* 3D 旋转
*
* @author Chen Xiaoping ([email protected])
* @version RecyclerCoverFlow
* @Datetime 2020-09-01 09:21
* @since RecyclerCoverFlow
*/
public class CoverFlow3DActivity extends AppCompatActivity implements Adapter.onItemClick {

private RecyclerCoverFlow mList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_just_coverflow);
initList();
}

private void initList() {
mList = findViewById(R.id.list);
mList.set3DItem(true); //3D 滚动
mList.setLoop(); //循环滚动
mList.setAdapter(new Adapter(this, this, true));
mList.setOnItemSelectedListener(new CoverFlowLayoutManger.OnSelected() {
@Override
public void onItemSelected(int position) {
((TextView)findViewById(R.id.index)).setText((position+1)+"/"+mList.getLayoutManager().getItemCount());
}
});
}

@Override
public void clickItem(int pos) {
mList.smoothScrollToPosition(pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void initList() {
// mList.setGreyItem(true); //设置灰度渐变
// mList.setAlphaItem(true); //设置半透渐变
mList.setLoop(); //循环滚动,注:循环滚动模式暂不支持平滑滚动
mList.setAdapter(new Adapter(this, this));
mList.setAdapter(new Adapter(this, this, false));
mList.setOnItemSelectedListener(new CoverFlowLayoutManger.OnSelected() {
@Override
public void onItemSelected(int position) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/recycler/coverflow/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public void onRecyclerViewClick(View view) {
startActivity(intent);
}

public void on3DCoverFlowClick(View view) {
Intent intent = new Intent(this, CoverFlow3DActivity.class);
startActivity(intent);
}

}
97 changes: 97 additions & 0 deletions app/src/main/java/com/recycler/coverflow/MirrorView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.recycler.coverflow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;

public class MirrorView extends android.support.v7.widget.AppCompatImageView {

public MirrorView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public MirrorView(Context context) {
this(context, null, 0);
}

public MirrorView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (getDrawable() != null) {
doReflection(((BitmapDrawable) getDrawable()).getBitmap());
}
}

@Override
public void setImageBitmap(Bitmap bm) {
doReflection(bm);
}

@Override
public void setImageDrawable(@Nullable Drawable drawable) {
if (drawable == null) return;
if (drawable instanceof GlideBitmapDrawable) {
Bitmap bitmap = ((GlideBitmapDrawable) drawable).getBitmap();
if (bitmap != null) doReflection(bitmap);
} else {
super.setImageDrawable(drawable);
}
}

@Override
public void setImageResource(int resId) {
doReflection(BitmapFactory.decodeResource(getResources(), resId));
}

private void doReflection(Bitmap originalImage) {
if (originalImage == null) return;
final int reflectionGap = 4;
int width = originalImage.getWidth();
int height = originalImage.getHeight();

Matrix matrix = new Matrix();
matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);

canvas.drawBitmap(originalImage, 0, 0, null);

Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00000000,
Shader.TileMode.MIRROR);

paint.setShader(shader);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
super.setImageDrawable(new BitmapDrawable(getResources(), bitmapWithReflection));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void onBindViewHolder(ViewHolder holder, int position) {
}

private void intiCoverFlow(final ViewHolder holder) {
holder.coverFlow.setAdapter(new Adapter(holder.itemView.getContext()));
holder.coverFlow.setAdapter(new Adapter(holder.itemView.getContext(), false));
holder.coverFlow.setOnItemSelectedListener(new CoverFlowLayoutManger.OnSelected() {
@Override
public void onItemSelected(int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void initList(View rootView) {
// mList.setFlatFlow(true); //平面滚动
mList.setGreyItem(true); //设置灰度渐变
// mList.setAlphaItem(true); //设置半透渐变
mList.setAdapter(new Adapter(getActivity()));
mList.setAdapter(new Adapter(getActivity(), false));
mList.setOnItemSelectedListener(new CoverFlowLayoutManger.OnSelected() {
@Override
public void onItemSelected(int position) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
android:layout_marginTop="10dp"
android:text="还有Recyvler View"
android:onClick="onRecyclerViewClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="3D Cover Flow"
android:onClick="on3DCoverFlowClick"/>
</LinearLayout>
16 changes: 16 additions & 0 deletions app/src/main/res/layout/layout_item_mirror.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:cardElevation="1dp">
<com.recycler.coverflow.MirrorView
android:id="@+id/img"
android:layout_width="150dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
tools:src="@mipmap/item1"/>
</FrameLayout>
Loading

0 comments on commit e63ac14

Please sign in to comment.