package com.benben.orangecircle.widget;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.benben.commoncore.utils.BitmapUtils;
import com.benben.commoncore.utils.LogUtils;
import com.benben.commoncore.utils.ScreenUtils;
import com.benben.orangecircle.R;
import com.benben.orangecircle.ui.bean.MetroBean;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class SnakeView extends View {
private static Activity mContext;
private float orangeX = 0;
private float orangeY = 0;
private float marginLeft = 100; // 左侧距离
private float horizontalSize = 100; // 横向距离
private float verticalSize = 100; // 竖向距离
private float lastX = 0; // 上个绘制的坐标
private float lastY = 0; // 上个绘制的坐标
private int textColor = getResources().getColor(R.color.color_333333);
List<MetroBean.CircleBean> mList = new ArrayList<>();
private List<PointBean> mPointBeanList = new ArrayList<>();
private String line_color;
public SnakeView(Context context) {
super(context);
}
public SnakeView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SnakeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setList(List<MetroBean.CircleBean> list,String line_color) {
mList = list;
this.line_color=line_color;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
orangeX = getX();
int parentWidth = getRight() - getLeft();
horizontalSize = parentWidth / 6;
verticalSize = horizontalSize;
marginLeft = (parentWidth - horizontalSize * 4) / 2;
orangeY = getY();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPointBeanList.clear();
if (mList == null || mList.size() == 0) {
return;
}
if (mList.size() == 1) {
drawPoint(canvas, mList.get(0));
} else {
for (int i = 0; i < mList.size(); i++) {
switch ((i + 1) % 6) {
case 0:
LogUtils.e("TAG","111111111111");
if (mList.size() - 1 == i) {
LogUtils.e("TAG","22222222222222");
drawRight(canvas, mList.get(i), lastX, lastY, false);
} else {
LogUtils.e("TAG","333333333333333");
drawRightDown(canvas, mList.get(i), lastX, lastY);
}
break;
case 1:
LogUtils.e("TAG","44444444444444");
if (mList.size() - 1 == i) {
LogUtils.e("TAG","5555555555555555");
drawDown(canvas, mList.get(i), lastX, lastY);
} else if (i == 0) {
LogUtils.e("TAG","666666666666666666666");
drawRight(canvas, mList.get(i), lastX, lastY, true);
} else {
LogUtils.e("TAG","77777777777777777777");
drawDownRight(canvas, mList.get(i), lastX, lastY);
}
break;
case 2:
LogUtils.e("TAG","8888888888888");
if (mList.size() - 1 == i) {
LogUtils.e("TAG","9999999999999999");
drawLeft(canvas, mList.get(i), lastX, lastY, true);
} else {
LogUtils.e("TAG","101010101010");
drawLeftAndRight(canvas, mList.get(i), lastX, lastY, true);
}
break;
case 3:
LogUtils.e("TAG","121212121212");
if (mList.size() - 1 == i) {
LogUtils.e("TAG","1313131313");
drawLeft(canvas, mList.get(i), lastX, lastY, true);
} else {
LogUtils.e("TAG","1414141414");
drawLeftDown(canvas, mList.get(i), lastX, lastY);
}
break;
case 4:
LogUtils.e("TAG","1515151515");
if (mList.size() - 1 == i) {
LogUtils.e("TAG","1616161616");
drawDown(canvas, mList.get(i), lastX, lastY);
} else {
LogUtils.e("TAG","1717171717");
drawDownLeft(canvas, mList.get(i), lastX, lastY);
}
break;
case 5:
LogUtils.e("TAG","1818181818");
if (mList.size() - 1 == i) {
LogUtils.e("TAG","1919191919");
drawRight(canvas, mList.get(i), lastX, lastY, false);
} else {
LogUtils.e("TAG","2020202020202020");
drawLeftAndRight(canvas, mList.get(i), lastX, lastY, false);
}
break;
}
}
}
}
// 绘制一个点: 0
private void drawPoint(Canvas canvas, MetroBean.CircleBean text) {
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), orangeX + marginLeft - textMiddle, orangeY + marginLeft - 50, paintTxt);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),orangeX + marginLeft-30, orangeY + marginLeft -30,new Paint());
}else {
new Thread(new Runnable() {
@Override
public void run() {
LogUtils.e("TAG",text.getAvatar());
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/*Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.addCircle(orangeX + marginLeft,orangeY + marginLeft,20,Path.Direction.CCW);
path.addCircle(orangeX + marginLeft,orangeY + marginLeft,10,Path.Direction.CW);
Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
paintPoint.setColor(getResources().getColor(R.color.theme));
canvas.drawPath(path,paintPoint);*/
PointBean pointBean = new PointBean(text.getId() + "", orangeX + marginLeft, orangeY + marginLeft);
mPointBeanList.add(pointBean);
}
// |
// 0
private void drawDown(Canvas canvas, MetroBean.CircleBean text, float startX, float startY) {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(startX, startY + verticalSize - 10);
canvas.drawPath(path, paintLine);
/* Path pathCircle = new Path();
pathCircle.setFillType(Path.FillType.EVEN_ODD);
pathCircle.addCircle(startX, startY + verticalSize, 20, Path.Direction.CCW);
pathCircle.addCircle(startX, startY + verticalSize, 10, Path.Direction.CW);
Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
paintPoint.setColor(getResources().getColor(R.color.theme));
canvas.drawPath(pathCircle, paintPoint);*/
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX - textMiddle, startY + verticalSize + 80, paintTxt);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),orangeX -30, startY + verticalSize -30,new Paint());
}else {
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
PointBean pointBean = new PointBean(text.getId() + "", startX, startY + verticalSize);
mPointBeanList.add(pointBean);
}
// 0-
private void drawRight(Canvas canvas, MetroBean.CircleBean text, float startX, float startY, boolean fromLeft) {
if (fromLeft) {
// 绘制点
// Path pathCircle = new Path();
// pathCircle.setFillType(Path.FillType.EVEN_ODD);
// pathCircle.addCircle(orangeX + marginLeft, orangeY, 20, Path.Direction.CCW);
// pathCircle.addCircle(orangeX + marginLeft, orangeY, 10, Path.Direction.CW);
// Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paintPoint.setColor(getResources().getColor(R.color.theme));
// canvas.drawPath(pathCircle, paintPoint);
// 绘制线
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(orangeX + marginLeft + 10, orangeY);
path.lineTo(orangeX + marginLeft + horizontalSize, orangeY);
canvas.drawPath(path, paintLine);
// 绘制文字
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), orangeX + marginLeft - textMiddle, orangeY - 50, paintTxt);
lastX = orangeX + marginLeft + horizontalSize;
lastY = orangeY;
PointBean pointBean = new PointBean(text.getId() + "", orangeX + marginLeft, orangeY);
mPointBeanList.add(pointBean);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),orangeX + marginLeft-30, orangeY -30,new Paint());
}else {
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
} else {
// 上个点在右边
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX + 10, startY);
path.lineTo(startX - horizontalSize + 10, startY);
canvas.drawPath(path, paintLine);
/* Path pathCircle = new Path();
pathCircle.setFillType(Path.FillType.EVEN_ODD);
pathCircle.addCircle(startX - horizontalSize, startY, 20, Path.Direction.CCW);
pathCircle.addCircle(startX - horizontalSize, startY, 10, Path.Direction.CW);
Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
paintPoint.setColor(getResources().getColor(R.color.theme));
canvas.drawPath(pathCircle, paintPoint);*/
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX - horizontalSize-30, startY -30,new Paint());
}else {
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX - horizontalSize - textMiddle, startY - 50, paintTxt);
PointBean pointBean = new PointBean(text.getId() + "", startX - horizontalSize, startY);
mPointBeanList.add(pointBean);
}
}
// -0-
private void drawLeftAndRight(Canvas canvas, MetroBean.CircleBean text, float startX, float startY, boolean fromLeft) {
if (fromLeft) {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(startX + horizontalSize - 10, startY);
canvas.drawPath(path, paintLine);
Path path2 = new Path();
path2.moveTo(startX + horizontalSize + 10, startY);
path2.lineTo(startX + horizontalSize + 20 + horizontalSize, startY);
canvas.drawPath(path2, paintLine);
/*Path pathCircle = new Path();
pathCircle.setFillType(Path.FillType.EVEN_ODD);
pathCircle.addCircle(startX + horizontalSize, startY, 20, Path.Direction.CCW);
pathCircle.addCircle(startX + horizontalSize, startY, 10, Path.Direction.CW);
Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
paintPoint.setColor(getResources().getColor(R.color.theme));
canvas.drawPath(pathCircle, paintPoint);*/
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX + horizontalSize-30, startY -30,new Paint());
}else {
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX + horizontalSize - textMiddle, startY - 50, paintTxt);
lastX = startX + horizontalSize + 10 + horizontalSize;
lastY = startY;
PointBean pointBean = new PointBean(text.getId() + "", startX + horizontalSize, startY);
mPointBeanList.add(pointBean);
} else {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(startX - horizontalSize + 10, startY);
canvas.drawPath(path, paintLine);
// Path pathCircle = new Path();
// pathCircle.setFillType(Path.FillType.EVEN_ODD);
// pathCircle.addCircle(startX - horizontalSize, startY, 20, Path.Direction.CCW);
// pathCircle.addCircle(startX - horizontalSize, startY, 10, Path.Direction.CW);
// Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paintPoint.setColor(getResources().getColor(R.color.theme));
// canvas.drawPath(pathCircle, paintPoint);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX - horizontalSize-30, startY -30,new Paint());
}else{
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Path pathLeft = new Path();
pathLeft.moveTo(startX - horizontalSize - 10, startY);
pathLeft.lineTo(startX - horizontalSize - horizontalSize - 10, startY);
canvas.drawPath(pathLeft, paintLine);
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX - horizontalSize - textMiddle, startY - 50, paintTxt);
lastX = startX - horizontalSize - horizontalSize - 10;
lastY = startY;
PointBean pointBean = new PointBean(text.getId() + "", startX - horizontalSize, startY);
mPointBeanList.add(pointBean);
}
}
// —— 0
private void drawLeft(Canvas canvas, MetroBean.CircleBean text, float startX, float startY, boolean fromLeft) {
if (fromLeft) {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(startX + horizontalSize - 10, startY);
canvas.drawPath(path, paintLine);
// Path pathCircle = new Path();
// pathCircle.setFillType(Path.FillType.EVEN_ODD);
// pathCircle.addCircle(startX + horizontalSize, startY, 20, Path.Direction.CCW);
// pathCircle.addCircle(startX + horizontalSize, startY, 10, Path.Direction.CW);
// Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paintPoint.setColor(getResources().getColor(R.color.theme));
// canvas.drawPath(pathCircle, paintPoint);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX + horizontalSize-30, startY -30,new Paint());
}else{
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX + horizontalSize - textMiddle, startY - 50, paintTxt);
lastX = startX + horizontalSize;
lastY = startY;
PointBean pointBean = new PointBean(text.getId() + "", startX + horizontalSize, startY);
mPointBeanList.add(pointBean);
} else {
}
}
// —— 0
// |
private void drawLeftDown(Canvas canvas, MetroBean.CircleBean text, float startX, float startY) {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(startX + horizontalSize - 10, startY);
canvas.drawPath(path, paintLine);
Path pathDown = new Path();
pathDown.moveTo(startX + horizontalSize, startY + 10);
pathDown.lineTo(startX + horizontalSize, startY + verticalSize);
canvas.drawPath(pathDown, paintLine);
/*Path pathCircle = new Path();
pathCircle.setFillType(Path.FillType.EVEN_ODD);
pathCircle.addCircle(startX + horizontalSize, startY, 20, Path.Direction.CCW);
pathCircle.addCircle(startX + horizontalSize, startY, 10, Path.Direction.CW);
Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
paintPoint.setColor(getResources().getColor(R.color.theme));
canvas.drawPath(pathCircle, paintPoint);*/
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX + horizontalSize-30, startY -30,new Paint());
}else{
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX + horizontalSize - textMiddle, startY - 50, paintTxt);
lastX = startX + horizontalSize;
lastY = startY + verticalSize;
PointBean pointBean = new PointBean(text.getId() + "", startX + horizontalSize, startY);
mPointBeanList.add(pointBean);
}
// |
// —— 0
private void drawDownLeft(Canvas canvas, MetroBean.CircleBean text, float startX, float startY) {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(startX, startY + verticalSize - 10);
canvas.drawPath(path, paintLine);
Path pathLeft = new Path();
pathLeft.moveTo(startX - 10, startY + verticalSize);
pathLeft.lineTo(startX - horizontalSize - 10, startY + verticalSize);
canvas.drawPath(pathLeft, paintLine);
// Path pathCircle = new Path();
// pathCircle.setFillType(Path.FillType.EVEN_ODD);
// pathCircle.addCircle(startX, startY + verticalSize, 20, Path.Direction.CCW);
// pathCircle.addCircle(startX, startY + verticalSize, 10, Path.Direction.CW);
// Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paintPoint.setColor(getResources().getColor(R.color.theme));
// canvas.drawPath(pathCircle, paintPoint);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX-30, startY + verticalSize -30,new Paint());
}else{
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX - textMiddle, startY + verticalSize + 80, paintTxt);
lastX = startX - horizontalSize - 10;
lastY = startY + verticalSize;
PointBean pointBean = new PointBean(text.getId() + "", startX, startY + verticalSize);
mPointBeanList.add(pointBean);
}
// 0 ——
// |
private void drawRightDown(Canvas canvas, MetroBean.CircleBean text, float startX, float startY) {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX + 10, startY);
path.lineTo(startX - horizontalSize + 10, startY);
canvas.drawPath(path, paintLine);
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX - horizontalSize - textMiddle, startY - 50, paintTxt);
Path pathDown = new Path();
pathDown.moveTo(startX - horizontalSize, startY + 10);
pathDown.lineTo(startX - horizontalSize, startY + verticalSize);
canvas.drawPath(pathDown, paintLine);
// Path pathCircle = new Path();
// pathCircle.setFillType(Path.FillType.EVEN_ODD);
// pathCircle.addCircle(startX - horizontalSize, startY, 20, Path.Direction.CCW);
// pathCircle.addCircle(startX - horizontalSize, startY, 10, Path.Direction.CW);
// Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paintPoint.setColor(getResources().getColor(R.color.theme));
// canvas.drawPath(pathCircle, paintPoint);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX - horizontalSize-30, startY -30,new Paint());
}else{
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
lastX = startX - horizontalSize;
lastY = startY + verticalSize;
PointBean pointBean = new PointBean(text.getId() + "", startX - horizontalSize, startY);
mPointBeanList.add(pointBean);
}
// |
// 0 ——
private void drawDownRight(Canvas canvas, MetroBean.CircleBean text, float startX, float startY) {
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setColor(Color.parseColor("#"+line_color));
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(startX, startY + verticalSize - 10);
canvas.drawPath(path, paintLine);
Path pathRight = new Path();
pathRight.moveTo(startX + 10, startY + verticalSize);
pathRight.lineTo(startX + horizontalSize + 10, startY + verticalSize);
canvas.drawPath(pathRight, paintLine);
// Path pathCircle = new Path();
// pathCircle.setFillType(Path.FillType.EVEN_ODD);
// pathCircle.addCircle(startX, startY + verticalSize, 20, Path.Direction.CCW);
// pathCircle.addCircle(startX, startY + verticalSize, 10, Path.Direction.CW);
// Paint paintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paintPoint.setColor(getResources().getColor(R.color.theme));
// canvas.drawPath(pathCircle, paintPoint);
if (text.getBitmap() != null){
canvas.drawBitmap(text.getBitmap(),startX-30, startY + verticalSize -30,new Paint());
}else{
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(text.getAvatar());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
float btWidth = bitmap.getWidth();
float btHeight = bitmap.getHeight();
// 水平方向开始裁剪的位置
float btWidthCutSite = 0;
// 竖直方向开始裁剪的位置
float btHeightCutSite = 0;
// 裁剪成正方形图片的边长,未拉伸缩放
float squareWidth = 0f;
if(btWidth > btHeight) { // 如果矩形宽度大于高度
btWidthCutSite = (btWidth - btHeight) / 3f;
squareWidth = btHeight;
} else { // 如果矩形宽度不大于高度
btHeightCutSite = (btHeight - btWidth) / 3f;
squareWidth = btWidth;
}
// 设置拉伸缩放比
float scale = 60 * 1.0f / squareWidth;
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
// 将矩形图片裁剪成正方形并拉伸缩放到控件大小
Bitmap squareBt = Bitmap.createBitmap(bitmap, (int)btWidthCutSite, (int)btHeightCutSite, (int)squareWidth, (int)squareWidth, matrix, true);
text.setBitmap(BitmapUtils.circleBitmap(squareBt));;
invalidate();
}
});
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Paint paintTxt = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTxt.setColor(textColor);
paintTxt.setTextSize(30);
int textMiddle = (text.getName().length() * 30) / 2;
canvas.drawText(text.getName(), startX - textMiddle, startY + verticalSize + 80, paintTxt);
lastX = startX + horizontalSize + 10;
lastY = startY + verticalSize;
PointBean pointBean = new PointBean(text.getId() + "", startX, startY + verticalSize);
mPointBeanList.add(pointBean);
}
public void setTextColor(int textColor) {
this.textColor = textColor;
invalidate();
}
private boolean isMove = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
isMove = true;
case MotionEvent.ACTION_UP:
if (isMove) {
isMove = false;
break;
}
for (int i = 0; i < mPointBeanList.size(); i++) {
float pointX = mPointBeanList.get(i).getPointX();
float pointY = mPointBeanList.get(i).getPointY();
float minX = pointX - horizontalSize;
float maxX = pointX + horizontalSize;
float minY = pointY - verticalSize;
float maxY = pointY + verticalSize;
if (event.getX() > minX && event.getX() < maxX && event.getY() > minY && event.getY() < maxY) {
mOnClickListener.onClick(mPointBeanList.get(i).getCircler_id() + "");
}
}
break;
}
return true;
}
public interface onClickListener {
void onClick(String id);
}
private onClickListener mOnClickListener;
public void setOnClickListener(onClickListener onClickListener) {
mOnClickListener = onClickListener;
}
// 记录每个点的信息(坐标 圈子ID)
public class PointBean {
String circler_id;
private float pointX;
private float pointY;
public PointBean(String circler_id, float pointX, float pointY) {
this.circler_id = circler_id;
this.pointX = pointX;
this.pointY = pointY;
}
public String getCircler_id() {
return circler_id;
}
public void setCircler_id(String circler_id) {
this.circler_id = circler_id;
}
public float getPointX() {
return pointX;
}
public void setPointX(float pointX) {
this.pointX = pointX;
}
public float getPointY() {
return pointY;
}
public void setPointY(float pointY) {
this.pointY = pointY;
}
}
public void setContext(Activity context){
mContext=context;
}
}
自定义view 地铁图
于 2020-12-25 16:58:06 首次发布