经过鄙人两年的大学生活,要画线的课程设计或实验遇到过两次,前些天课程设计选择的安卓搞界面,遇到过很多困难,还要几天之内搞定,界面好坑,就想写一下。
必须的,之前我有想过用一个ImageView代替然后给这个控件加一张斜线图片,最终,浪费了我两天时间。
这个画布我不想多说什么,必须用RelativeLayout布局!!!!!!!!直接上代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class NewView extends View {
private float startX;
private float startY;
private float stopX;
private float stopY;
private float startXX;
private float startYY;
private float stopXX;
private float stopYY;
private int distance = 0;
private String connetRouter;
private String connectNet;
private Bitmap bitmp ;
public NewView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NewView(luyouView luyouview, float startX, float startY, float stopX, float stopY) {
super(luyouview);
this.startX = startX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
bitmp =Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
}
// 当Android系统需要绘制一个View对象时,就会嗲用该对象的onDraw
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.parseColor("#00FFFFFF"));
Paint paint = new Paint();
paint.setColor(Color.BLACK);
if (startX < stopX) {
if (startY > stopY){
canvas.drawLine(0, startY-stopY, stopX-startX,0, paint);
}
if (startY < stopY){
canvas.drawLine(0, 0, stopX - startX,stopY-startY, paint);
}
}
if(startX > stopX) {
if (startY > stopY) {
canvas.drawLine(startX - stopX, startY - stopY, 0,0, paint);
}
if (startY < stopY) {
canvas.drawLine(0, stopY - startY, startX - stopX,0, paint);
}
}
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//设置宽高
setMeasuredDimension((int)Math.abs(stopX-startX), (int)Math.abs(stopY-startY));
}
public void setStartXX(float startXX) {
this.startXX = startXX;
}
public void setStartYY(float startYY) {
this.startYY = startYY;
}
public void setStopXX(float stopXX) {
this.stopXX = stopXX;
}
public void setStopYY(float stopYY) {
this.stopYY = stopYY;
}
public void setDistance(int distance) {
this.distance = distance;
}
public void setConnetRouter(String connetRouter) {
this.connetRouter = connetRouter;
}
public void setConnectNet(String connectNet) {
this.connectNet = connectNet;
}
public float getStartXX() {
return startXX;
}
public float getStartYY() {
return startYY;
}
public float getStopXX() {
return stopXX;
}
public float getStopYY() {
return stopYY;
}
public int getDistance() {
return distance;
}
public String getConnetRouter() {
return connetRouter;
}
public String getConnectNet() {
return connectNet;
}
}
这个类中用来画线的就只有其中的onDraw函数了,其它的get和set都是我的课程设计需要的东西。这个类的主要作用就是:画线,并且缩小view至其与线条在xy轴上的投影长宽相等的位置,但是,这个线条画出来之后,View的位置在左上角。
主程序对其的调用:
NewView newView = new NewView(luyouView.this, startX, startY, stopX, stopY);
relativeLayout.addView(newView);
主程序名字是luyouView,替换一下就行了,至于startX等等,就是另一段代码了,那块代码关系甚大,附上去你们肯定运行不了。这里的startX、startY、stopX、stopY就是画线的时候监控的手指起点和终点的x、y的坐标。最后你们移动一下view就行了。
移动View的代码:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
newView.getLayoutParams();
params.height = newView.getHeight();
params.width = newView.getWidth();
if (startX < stopX) {
if (startY >stopY){
params.setMargins((int) startX,(int) stopY, (int)stopX, (int) startY);
}
if (startY < stopY){
params.setMargins((int) startX, (int) startY, (int) stopX, (int) stopY);
}
}
if(startX > stopX) {
if (startY > stopY) {
params.setMargins((int) stopX, (int) stopY, (int) startX, (int) startY);
}
if (startY < stopY) {
params.setMargins((int) stopX, (int) startY, (int)startX, (int) stopY);
}
}
newView.setLayoutParams(params);
newView.setId(newViewOnlyId);
newView.setStartXX(startX);
newView.setStartYY(startY);
newView.setStopXX(stopX);
newView.setStopYY(stopY);
newViewOnlyId++;
if (connectInformation.get(0)[2] == 0)
newView.setConnectNet(listNetButton.get(connectInformation.get(0)[0]).getText().toString());
if (connectInformation.get(0)[2] == 1)
newView.setConnetRouter(listRouterButton.get(connectInformation.get(0)[0]).getText().toString());
if (connectInformation.get(1)[2] == 0)
newView.setConnectNet(listNetButton.get(connectInformation.get(1)[0]).getText().toString());
if (connectInformation.get(1)[2] == 1)
newView.setConnetRouter(listRouterButton.get(connectInformation.get(1)[0]).getText().toString());
connectInformation = new ArrayList<>();
for (int i = 0; i < routers.size(); i++) {
if (routers.get(i).getRouterName().equals(newView.getConnetRouter())){
ArrayList connectNets = routers.get(i).getConnectNets();
connectNets.add(newView.getConnectNet());
routers.get(i).setConnectNets(connectNets);
setRouters(i);
break;
}
}
listNewView.add(newView);
if (((startX > stopX) && (startY > stopY)) || ((startX < stopX) && (startY < stopY))) {
new PathInfo.Builder(new TopLeftAndButtomRight(), newView)
.setApplyFlag(APPLY_FLAG_DRAW_AND_TOUCH)
.setClipType(CLIP_TYPE_IN)
.create()
.apply();
}else {
new PathInfo.Builder(new ButtomLeftAndTopRight(), newView)
.setApplyFlag(APPLY_FLAG_DRAW_AND_TOUCH)
.setClipType(CLIP_TYPE_IN)
.create()
.apply();
}
// setContentView(newView); //狗屁的setContentView,浪费我这么多时间
listNewView就是NewView的变长数组,最后的那段if else代码引用了别人写的控件切割布局,链接
这作用呢,当然是把长方形的View切割成我想要的形状。