feat: 优化代码

pull/134/head^2
kunfei 5 years ago
parent e903c01b4e
commit 27aa1ff662
  1. 93
      app/src/main/java/io/legado/app/ui/widget/ArcView.java
  2. 82
      app/src/main/java/io/legado/app/ui/widget/ArcView.kt
  3. 2
      app/src/main/java/io/legado/app/ui/widget/text/ScrollTextView.kt
  4. 2
      app/src/main/res/layout/activity_book_info.xml

@ -1,93 +0,0 @@
package io.legado.app.ui.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import io.legado.app.R;
import androidx.annotation.Nullable;
public class ArcView extends View {
private int mWidth;
private int mHeight;
/**
* 弧形高度
*/
private int mArcHeight;
/**
* 背景颜色
*/
private int mBgColor;
private Paint mPaint;
private boolean mDirectionTop;
private Context mContext;
public ArcView(Context context) {
this(context, null);
}
public ArcView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcView);
mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcView_arcHeight, 0);
mBgColor = typedArray.getColor(R.styleable.ArcView_bgColor, Color.parseColor("#303F9F"));
mDirectionTop = typedArray.getBoolean(R.styleable.ArcView_arcDirectionTop, false);
mContext = context;
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mBgColor);
if (mDirectionTop) {
Rect rect = new Rect(0, mArcHeight, mWidth, mHeight);
canvas.drawRect(rect, mPaint);
Path path = new Path();
path.moveTo(0, mArcHeight);
path.quadTo(mWidth / 2, 0, mWidth, mArcHeight);
canvas.drawPath(path, mPaint);
} else {
Rect rect = new Rect(0, 0, mWidth, mHeight - mArcHeight);
canvas.drawRect(rect, mPaint);
Path path = new Path();
path.moveTo(0, mHeight - mArcHeight);
path.quadTo(mWidth / 2, mHeight, mWidth, mHeight - mArcHeight);
canvas.drawPath(path, mPaint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
mWidth = widthSize;
}
if (heightMode == MeasureSpec.EXACTLY) {
mHeight = heightSize;
}
setMeasuredDimension(mWidth, mHeight);
}
}

@ -0,0 +1,82 @@
package io.legado.app.ui.widget
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import io.legado.app.R
class ArcView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private var mWidth = 0
private var mHeight = 0
/**
* 弧形高度
*/
private val mArcHeight: Int
/**
* 背景颜色
*/
private val mBgColor: Int
private val mPaint: Paint = Paint()
private val mDirectionTop: Boolean
val rect = Rect()
val path = Path()
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
mPaint.style = Paint.Style.FILL
mPaint.color = mBgColor
if (mDirectionTop) {
rect.set(0, mArcHeight, mWidth, mHeight)
canvas.drawRect(rect, mPaint)
path.reset()
path.moveTo(0f, mArcHeight.toFloat())
path.quadTo(mWidth / 2.toFloat(), 0f, mWidth.toFloat(), mArcHeight.toFloat())
canvas.drawPath(path, mPaint)
} else {
rect.set(0, 0, mWidth, mHeight - mArcHeight)
canvas.drawRect(rect, mPaint)
path.reset()
path.moveTo(0f, mHeight - mArcHeight.toFloat())
path.quadTo(
mWidth / 2.toFloat(),
mHeight.toFloat(),
mWidth.toFloat(),
mHeight - mArcHeight.toFloat()
)
canvas.drawPath(path, mPaint)
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val heightSize = MeasureSpec.getSize(heightMeasureSpec)
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
if (widthMode == MeasureSpec.EXACTLY) {
mWidth = widthSize
}
if (heightMode == MeasureSpec.EXACTLY) {
mHeight = heightSize
}
setMeasuredDimension(mWidth, mHeight)
}
init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcView)
mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcView_arcHeight, 0)
mBgColor = typedArray.getColor(
R.styleable.ArcView_bgColor,
Color.parseColor("#303F9F")
)
mDirectionTop = typedArray.getBoolean(R.styleable.ArcView_arcDirectionTop, false)
typedArray.recycle()
}
}

@ -1,4 +1,4 @@
package io.legado.app.ui.widget package io.legado.app.ui.widget.text
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context

@ -243,7 +243,7 @@
</LinearLayout> </LinearLayout>
<io.legado.app.ui.widget.ScrollTextView <io.legado.app.ui.widget.text.ScrollTextView
android:id="@+id/tv_intro" android:id="@+id/tv_intro"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_width="match_parent" android:layout_width="match_parent"

Loading…
Cancel
Save