Commit 91505e27 by 刘鹏

feat: 首页轮播动画

parent fdb200c4
package com.yidianling.home.ui.transform;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
import jp.wasabeef.glide.transformations.BitmapTransformation;
public class CircleCropTransform extends BitmapTransformation {
private final Paint mBorderPaint;
private final float mBorderSize;
public CircleCropTransform() {
this(0, Color.TRANSPARENT);
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
/**
* @param borderSize 边框宽度(px)
* @param borderColor 边框颜色
*/
public CircleCropTransform(float borderSize, int borderColor) {
this(TypedValue.COMPLEX_UNIT_PX, borderSize, borderColor);
}
/**
* @param unit borderSize 单位
* @param borderSize 边框宽度(px)
* @param borderColor 边框颜色
*/
public CircleCropTransform(int unit, float borderSize, int borderColor) {
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mBorderSize = TypedValue.applyDimension(unit, borderSize, displayMetrics);
mBorderPaint = new Paint();
mBorderPaint.setDither(true);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(borderColor);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setStrokeWidth(mBorderSize);
}
private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = (int) (Math.min(source.getWidth(), source.getHeight()) - (mBorderSize / 2));
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
if (mBorderPaint != null) {
float borderRadius = r - mBorderSize / 2;
canvas.drawCircle(r, r, borderRadius, mBorderPaint);
}
return result;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
@Override
public boolean equals(Object o) {
return false;
}
@Override
public int hashCode() {
return 0;
}
}
...@@ -4,6 +4,7 @@ import android.animation.Animator ...@@ -4,6 +4,7 @@ import android.animation.Animator
import android.animation.AnimatorListenerAdapter import android.animation.AnimatorListenerAdapter
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.graphics.Color
import android.os.Handler import android.os.Handler
import android.os.Message import android.os.Message
import android.util.AttributeSet import android.util.AttributeSet
...@@ -11,7 +12,9 @@ import android.util.Log ...@@ -11,7 +12,9 @@ import android.util.Log
import android.widget.ImageView import android.widget.ImageView
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.blankj.utilcode.util.SizeUtils import com.blankj.utilcode.util.SizeUtils
import com.ydl.ydl_image.module.GlideApp
import com.yidianling.home.R import com.yidianling.home.R
import com.yidianling.home.ui.transform.CircleCropTransform
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
const val START_AVATAR_LOOP = 111 const val START_AVATAR_LOOP = 111
...@@ -98,7 +101,7 @@ class LoopScrollAvatar @JvmOverloads constructor( ...@@ -98,7 +101,7 @@ class LoopScrollAvatar @JvmOverloads constructor(
* 创建圆形头像ImageView * 创建圆形头像ImageView
*/ */
private fun createImageView(): ImageView { private fun createImageView(): ImageView {
return XCRoundImageView(context) return ImageView(context)
} }
/** /**
...@@ -114,9 +117,6 @@ class LoopScrollAvatar @JvmOverloads constructor( ...@@ -114,9 +117,6 @@ class LoopScrollAvatar @JvmOverloads constructor(
//当前已在屏幕显示的控件不要复用,防止params混乱 //当前已在屏幕显示的控件不要复用,防止params混乱
ivCache.remove(iv) ivCache.remove(iv)
iv.setImageResource(res[index])
iv.scaleType = ImageView.ScaleType.FIT_XY
//图片资源全部播放完之后要从头重播 //图片资源全部播放完之后要从头重播
index = (index + 1) % res.size index = (index + 1) % res.size
...@@ -126,6 +126,9 @@ class LoopScrollAvatar @JvmOverloads constructor( ...@@ -126,6 +126,9 @@ class LoopScrollAvatar @JvmOverloads constructor(
iv.layoutParams = lp iv.layoutParams = lp
addView(iv) addView(iv)
//圆角带描边
GlideApp.with(this).load(res[index]).transform(CircleCropTransform(1f, Color.WHITE))
.into(iv)
} }
/** /**
......
package com.yidianling.home.ui.view
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
/**
* @author liupeng
*/
class XCRoundImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : AppCompatImageView(
context, attrs, defStyle
) {
private val paint: Paint = Paint()
/**
* 绘制圆形图片
* @author caizhiming
*/
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) {
val drawable = drawable
if (null != drawable) {
val bitmap = (drawable as BitmapDrawable).bitmap
val b = getCircleBitmap(bitmap, 14)
val rectSrc = Rect(0, 0, b.width, b.height)
val rectDest = Rect(0, 0, width, height)
paint.reset()
canvas.drawBitmap(b, rectSrc, rectDest, paint)
} else {
super.onDraw(canvas)
}
}
/**
* 获取圆形图片方法
* @param bitmap
* @param pixels
* @return Bitmap
* @author caizhiming
*/
private fun getCircleBitmap(bitmap: Bitmap, pixels: Int): Bitmap {
val output = Bitmap.createBitmap(
bitmap.width,
bitmap.height, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
val color = -0xbdbdbe
val rect = Rect(0, 0, bitmap.width, bitmap.height)
paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
paint.color = color
val x = bitmap.width
canvas.drawCircle((x / 2).toFloat(), (x / 2).toFloat(), (x / 2).toFloat(), paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
return output
}
}
\ No newline at end of file
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
android:src="@mipmap/home_banner_header1" android:src="@mipmap/home_banner_header1"
app:layout_constraintTop_toBottomOf="@id/homeModuleFirstDec" app:layout_constraintTop_toBottomOf="@id/homeModuleFirstDec"
app:layout_constraintLeft_toLeftOf="@id/homeModuleButtonBannerFirstTitle" app:layout_constraintLeft_toLeftOf="@id/homeModuleButtonBannerFirstTitle"
android:layout_width="50dp" android:layout_width="45dp"
app:layout_constraintHorizontal_weight="1" app:layout_constraintHorizontal_weight="1"
android:layout_height="20dp" /> android:layout_height="20dp" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment