Skip to content

Commit

Permalink
adjust color
Browse files Browse the repository at this point in the history
use ImageView,init circle image
  • Loading branch information
qintong1 committed May 10, 2017
1 parent d6f8633 commit f243c25
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE75764"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
Expand All @@ -16,5 +15,6 @@
android:layout_centerInParent="true"
android:id="@+id/avi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import java.util.ArrayList;

import static android.graphics.Shader.TileMode.CLAMP;

public class InsLoadingView extends View {
public class InsLoadingView extends ImageView {
private static String TAG = "InsLoadingView";
private static boolean DEBUG = true;
private double circleDia = 0.9;
Expand Down Expand Up @@ -50,10 +55,15 @@ public InsLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {

@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
//super.onDraw(canvas);
if (DEBUG) {
Log.d(TAG, "onDraw " + getWidth() + "---" + getHeight());
}
Paint bitmapPaint = new Paint();
setBitmapShader(bitmapPaint);
RectF rectF = new RectF((float) (getWidth() * (1 - circleDia)), (float) (getWidth() * (1 - circleDia)),
(float) (getWidth() * circleDia), (float) (getHeight() * circleDia));
canvas.drawOval(rectF, bitmapPaint);
Paint paint = getPaint(getColor(0), getColor(360) , 360);
drawTrack(canvas, paint);
postInvalidate();
Expand Down Expand Up @@ -171,7 +181,6 @@ public void onAnimationRepeat(Animator animation) {
isFirstCircle = !isFirstCircle;
}
});

rotateAnim.start();
arcAnim.start();
circleDAnimator.start();
Expand All @@ -182,8 +191,8 @@ private static int getColor(double degree) {
if (degree < 0 || degree > 360) {
Log.d(TAG, "getColor error:" + degree);
}
int startColor = Color.YELLOW;
int endColor = Color.RED;
int startColor = Color.parseColor("#FFF700C2");
int endColor = Color.parseColor("#FFFFD900");
double radio = degree/360;
int redStart = Color.red(startColor);
int blueStart = Color.blue(startColor);
Expand All @@ -207,4 +216,43 @@ private Paint getPaint(int startColor, int endColor, double arcWidth) {
paint.setStrokeWidth(getHeight() * strokeWidth);
return paint;
}

/**
* 设置BitmapShader
* https://my.oschina.net/zhangqie/blog/794363
*/
private void setBitmapShader(Paint paint) {
Drawable drawable = getDrawable();
Matrix matrix = new Matrix();
if (null ==drawable) {
return;
}
Bitmap bitmap = drawableToBitmap(drawable);
// 将bitmap作为着色器来创建一个BitmapShader
BitmapShader tshader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
float scale =1.0f;
// 拿到bitmap宽或高的小值
int bSize =Math.min(bitmap.getWidth(), bitmap.getHeight());
scale = getWidth() * 1.0f /bSize;
// shader的变换矩阵,我们这里主要用于放大或者缩小
matrix.setScale(scale,scale);
// 设置变换矩阵
tshader.setLocalMatrix(matrix);
paint.setShader(tshader);

}

private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable =(BitmapDrawable) drawable;
return bitmapDrawable.getBitmap();
}
int w =drawable.getIntrinsicWidth();
int h =drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
}

0 comments on commit f243c25

Please sign in to comment.