91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android使用貝塞爾曲線仿QQ聊天消息氣泡拖拽效果

發布時間:2020-09-21 17:08:03 來源:腳本之家 閱讀:131 作者:小悅月CG 欄目:移動開發

本文實例為大家分享了Android仿QQ聊天消息氣泡拖拽效果展示的具體代碼,供大家參考,具體內容如下

先畫圓,都會吧。代碼如下:

public class Bezier extends View {
 private final Paint mGesturePaint = new Paint();
 private final Path mPath = new Path();
 private float mX1 = 100, mY1 = 150;
 private float mX2 = 300, mY2 = 150;
 private boolean mBezier = true;
 private int mRadius = 30;
 
 public Bezier(Context context, AttributeSet attrs) {
 super(context, attrs);
 mGesturePaint.setAntiAlias(true);
 mGesturePaint.setStyle(Paint.Style.FILL_AND_STROKE);
 mGesturePaint.setStrokeWidth(5);
 mGesturePaint.setColor(Color.RED);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 super.onDraw(canvas);
 canvas.drawCircle(mX1, mX2, mRadius, mGesturePaint);
 }
 
}

效果

Android使用貝塞爾曲線仿QQ聊天消息氣泡拖拽效果

拖拽的另個一圓就不畫了,效果的實現主要是計算兩個點之間的拖拽區域,如下圖:

Android使用貝塞爾曲線仿QQ聊天消息氣泡拖拽效果

求出區域之后,使用貝塞爾線畫出效果就可以了,代碼:

public class Bezier extends View {
 private final Paint mGesturePaint = new Paint();
 private final Path mPath = new Path();
 private float mX1 = 100, mY1 = 150;
 private float mX2 = 300, mY2 = 150;
 private boolean mBezier = true;
 private int mRadius = 30;
 
 public Bezier(Context context, AttributeSet attrs) {
 super(context, attrs);
 mGesturePaint.setAntiAlias(true);
 mGesturePaint.setStyle(Paint.Style.FILL_AND_STROKE);
 mGesturePaint.setStrokeWidth(5);
 mGesturePaint.setColor(Color.RED);
 setBezier();
 }
 
 private void setBezier() {
 float offsetX = (float) (mRadius * Math.sin(Math.atan((mY2 - mY1) / (mX2 - mX1))));
 float offsetY = (float) (mRadius * Math.cos(Math.atan((mY2 - mY1) / (mX2 - mX1))));
 float x1 = mX1 - offsetX;
 float y1 = mY1 + offsetY;
 
 float x2 = mX2 - offsetX;
 float y2 = mY2 + offsetY;
 
 float x3 = mX2 + offsetX;
 float y3 = mY2 - offsetY;
 
 float x4 = mX1 + offsetX;
 float y4 = mY1 - offsetY;
 
 mPath.reset();
 mPath.moveTo(x1, y1);
 mPath.quadTo((mX1 + mX2) / 2, (mY1 + mY2) / 2, x2, y2);//錨點直接取偏移量的一半
 mPath.lineTo(x3, y3);
 mPath.quadTo((mX1 + mX2) / 2, (mY1 + mY2) / 2, x4, y4);
 mPath.lineTo(x1, y1);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 super.onDraw(canvas);
 //通過畫布繪制多點形成的圖形
 canvas.drawCircle(mX1, mY1, mRadius, mGesturePaint);
 if (mBezier) {
 canvas.drawPath(mPath, mGesturePaint);
 canvas.drawCircle(mX2, mY2, mRadius, mGesturePaint);
 }
 }
}

效果圖:

Android使用貝塞爾曲線仿QQ聊天消息氣泡拖拽效果

拖拽效果只要在onTouchEvent里動態改變拖動點的坐標重繪就可以實現了,代碼:

public class Bezier extends View {
 private final Paint mGesturePaint = new Paint();
 private final Path mPath = new Path();
 private float mX1 = 100, mY1 = 150;
 private float mX2 = 300, mY2 = 150;
 private boolean mBezier = true;
 private int mRadius = 30;
 
 public Bezier(Context context, AttributeSet attrs) {
 super(context, attrs);
 mGesturePaint.setAntiAlias(true);
 mGesturePaint.setStyle(Paint.Style.FILL_AND_STROKE);
 mGesturePaint.setStrokeWidth(5);
 mGesturePaint.setColor(Color.RED);
 setBezier();
 }
 
 private void setBezier() {
 float offsetX = (float) (mRadius * Math.sin(Math.atan((mY2 - mY1) / (mX2 - mX1))));
 float offsetY = (float) (mRadius * Math.cos(Math.atan((mY2 - mY1) / (mX2 - mX1))));
 float x1 = mX1 - offsetX;
 float y1 = mY1 + offsetY;
 
 float x2 = mX2 - offsetX;
 float y2 = mY2 + offsetY;
 
 float x3 = mX2 + offsetX;
 float y3 = mY2 - offsetY;
 
 float x4 = mX1 + offsetX;
 float y4 = mY1 - offsetY;
 
 mPath.reset();
 mPath.moveTo(x1, y1);
 mPath.quadTo((mX1 + mX2) / 2, (mY1 + mY2) / 2, x2, y2);//錨點直接取偏移量的一半
 mPath.lineTo(x3, y3);
 mPath.quadTo((mX1 + mX2) / 2, (mY1 + mY2) / 2, x4, y4);
 mPath.lineTo(x1, y1);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 super.onDraw(canvas);
 //通過畫布繪制多點形成的圖形
 canvas.drawCircle(mX1, mY1, mRadius, mGesturePaint);
 if (mBezier) {
 canvas.drawPath(mPath, mGesturePaint);
 canvas.drawCircle(mX2, mY2, mRadius, mGesturePaint);
 }
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 mX2 = event.getX();
 mY2 = event.getY();
 switch (event.getAction()) {
 case MotionEvent.ACTION_DOWN:
 mBezier = true;
 setBezier();
 break;
 case MotionEvent.ACTION_MOVE:
 mBezier = true;
 setBezier();
 break;
 case MotionEvent.ACTION_UP:
 mBezier = false;
 break;
 
 }
 invalidate();
 return true;
 }
 
}

源碼:Android仿QQ聊天消息氣泡拖拽效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

莱芜市| 新化县| 绥阳县| 井研县| 芷江| 唐山市| 金寨县| 梨树县| 昌都县| 勐海县| 平果县| 庆元县| 清涧县| 平原县| 牟定县| 寿宁县| 巴中市| 阿拉善盟| 安乡县| 翼城县| 信宜市| 两当县| 临城县| 承德市| 宁河县| 临邑县| 上高县| 定陶县| 天等县| 内乡县| 枞阳县| 平湖市| 敖汉旗| 柯坪县| 化州市| 华蓥市| 宝清县| 霍城县| 百色市| 盐山县| 桂东县|