我的项目中有自定义视图。在视图类中,我使用onDraw方法绘制一条线。可以通过向上和向下滑动来改变线条的高度。如何设置该线条高度的最大值和最小值?
这是我的代码
package com.example.richyrony.draw;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
/**
* Created by Richy & Rony on 26-01-2018.
*/
public class Line extends View {
PointF topLeft = new PointF(100, 100);
PointF topRight = new PointF(290, 100);
PointF bottomLeft = new PointF(100, 290);
PointF bottomRight = new PointF(290, 290);
private final int NONE = -1, TOUCH_TOP_LEFT = 0, TOUCH_TOP_RIGHT = 1, TOUCH_BOT_LEFT = 2, TOUCH_BOT_RIGHT = 3;
int currentTouch = NONE;
Float lineTwoStartx = 250f;
Float lineTwoStart = 50f;
Float lineTwoEndx = 250f;
Float lineTwoEnd = 200f;
Float lineOneStartx = 50f;
Float lineOneStart = 50f;
;
Float lineOneEndx = 50f;
Float lineOneEnd = 200f;
RectF touchArea = new RectF(0, 0, 350, 350);
Paint paint;
public Line(Context context) {
super(context);
paint = new Paint();
paint.setStrokeWidth(4);
paint.setColor(Color.parseColor("#000000"));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(lineOneStartx, lineOneStart, lineOneEndx, lineOneEnd, paint);
canvas.drawLine(lineTwoStartx, lineTwoStart, lineTwoEndx, lineTwoEnd, paint);
canvas.drawLine(lineOneEndx, lineOneEnd, lineTwoEndx, lineTwoEnd, paint);
canvas.drawLine(lineOneStartx, lineOneStart, lineTwoStartx, lineTwoStart, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
//The user just put their finger down.
//We check to see which corner the user is touching
//And set our global, currentTouch, to the appropriate constant.
case MotionEvent.ACTION_DOWN:
if (touchArea.contains(event.getX(), event.getY())) {
currentTouch = TOUCH_TOP_LEFT;
Toast.makeText(getContext(), "tf" + currentTouch, Toast.LENGTH_SHORT).show();
} else {
return false; //Return false if user touches none of the corners
}
return true; //Return true if the user touches one of the corners
//Now we know which corner the user is touching.
//When the user moves their finger, we update the point to the user position and invalidate.
case MotionEvent.ACTION_MOVE:
switch (currentTouch) {
case TOUCH_TOP_LEFT:
lineOneEnd = event.getY();
invalidate();
return true;
}
//We returned true for all of the above cases, because we used the event
return false; //If currentTouch is none of the above cases, return false
//Here the user lifts up their finger.
//We update the points one last time, and set currentTouch to NONE.
case MotionEvent.ACTION_UP:
switch (currentTouch) {
case TOUCH_TOP_LEFT:
lineOneEnd = event.getY();
currentTouch = NONE;
invalidate();
return true;
}
return false;
}
return false;
}
}
如果有人知道如何测量两条线之间的角度,那将会很有帮助