Prechádzať zdrojové kódy

********************************** *********************
添加矩阵

zhangweixin 3 rokov pred
rodič
commit
e9415b5e01

+ 1 - 1
demo/src/main/java/com/sybotan/android/demo/graphy/GraphyWallItem.kt

@@ -91,7 +91,7 @@ class GraphyWallItem(pointList: List<PointF>, parent: SGraphyItem? = null) : SGr
      * @param   canvas      画布对象
      * @param   rect        更新区域
      */
-    override fun onDraw(canvas: Canvas, rect: RectF) {
+    override fun onDraw(canvas: Canvas) {
         if (pointList.size < 2) {       // 如果少于2个点,则退出
             return
         }

+ 1 - 1
gradle.properties

@@ -26,7 +26,7 @@ org.gradle.jvmargs = -Xmx1536m
 SAGA_URL = http://www.sagaloud.cn
 SAGA_KOTLIN_VERSION = 1.4.105
 SAGA_SERVICE_VERSION = 1.4.140
-SAGA_ANDROID_VERSION = 1.2.42
+SAGA_ANDROID_VERSION = 1.2.46
 SAGA_CODE = 5
 SAGA_GROUP = cn.sagacloud
 

+ 35 - 17
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/SGraphyItem.kt

@@ -31,6 +31,7 @@ import android.util.Log
 import com.sybotan.android.graphy.enums.SGraphyItemFlag
 import com.sybotan.android.graphy.listeners.SGraphyItemPosListener
 import com.sybotan.android.graphy.utils.MatrixTools
+import com.sybotan.base.extensions.toJson
 import org.jetbrains.anko.doAsync
 import java.util.*
 
@@ -276,15 +277,10 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
      * @return  在item中的坐标
      */
     fun mapFromScene(x: Float, y: Float): PointF {
-        val list = itemPath()
-        var x0 = x
-        var y0 = y
-        for (item in list) {
-            x0 = (x0 - item.pos.x) / item.scale.x
-            y0 = (y0 - item.pos.y) / item.scale.y
-        }
-
-        return PointF(x0, y0)
+        val m = this.scene2itemMattrix()
+        val matrix = Matrix()
+        m.invert(matrix)
+       return MatrixTools.matrixTransform(matrix,x,y)
     } // Function mapFromScene()
 
     /**
@@ -295,7 +291,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
      * @return  在item中的坐标
      */
     fun mapFromScene(point: PointF): PointF {
-        return PointF(point.x, point.y)
+        return mapFromScene(point.x, point.y)
     } // Function mapFromScene()
 
     /**
@@ -310,8 +306,9 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
         if (null == parent) {
             return PointF(x, y)
         }
-
-        return parent!!.mapToScene(x * scale.x + pos.x, y * scale.y + pos.y)
+        val m = this.scene2itemMattrix()
+        return return MatrixTools.matrixTransform(m,x,y)
+//        return parent!!.mapToScene(x * scale.x + pos.x, y * scale.y + pos.y)
     } // Function mapToScene()
 
     /**
@@ -322,7 +319,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
      * @return  在场景中的坐标
      */
     fun mapToScene(point: PointF): PointF {
-        return PointF(point.x, point.y)
+        return mapToScene(point.x, point.y)
     } // Function mapToScene()
 
     /**
@@ -371,8 +368,11 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
      * @param   canvas      画布对象
      * @param   rect        更新区域
      */
-    open fun onDraw(canvas : Canvas, rect: RectF)
-    {
+    open fun onPaint(canvas : Canvas, rect: RectF) {
+        canvas.save()
+        this.onDraw(canvas)
+        canvas.restore()
+        val src = kotlin.floatArrayOf(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f)
         for (i in 1 .. children.size) {        // 倒序依次取item列中的所有item。将所有item的边界做并交处理。
             val item = children[children.size - i]
             if (!item.isVisible) {      // 如果对象不可见
@@ -383,10 +383,19 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
                 canvas.save()
                 // item位移到指定位置绘制
                 canvas.translate(item.pos.x, item.pos.y)
+                canvas.scale(item.scale.x,item.scale.y)
+                canvas.rotate(item.rotate)
+
+                if (!item.isTransform) {
+                    val matrix = canvas.matrix
+                    matrix.getValues(src)
+                    item._inverseScale = 1.0f/src[0]
+                    canvas.scale(item._inverseScale,item._inverseScale)
+                }
                 // 设置绘制区域
                 // canvas.clipRect(item.boundingRect())
                 // 绘制item
-                item.onDraw(canvas, rect)
+                item.onPaint(canvas, rect)
                 // 恢复画布状态
                 canvas.restore()
             } catch (e: Exception) {
@@ -397,6 +406,15 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
     } // Function paint()
 
     /**
+     * Item 绘制操作
+     *
+     * @param painter    绘制对象
+     */
+    open fun onDraw(canvas : Canvas) {
+
+    }
+
+    /**
      * 获得焦点变更回调
      */
     open fun onGetFocus() {
@@ -447,7 +465,7 @@ open class SGraphyItem(parent: SGraphyItem? = null) {
     open fun onSingleTapUp(e: SMotionEvent): Boolean {
         Log.d(TAG, "onSingleTapUp: releaseItem")
         releaseItem()
-
+        Log.e("手势scene",e.toJson())
         for (item in children) {
             if (!item.isVisible) {      // 如果对象不可见
                 continue

+ 6 - 2
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/SGraphyScene.kt

@@ -32,6 +32,7 @@ import com.sybotan.android.graphy.items.SGraphyImageItem
 import com.sybotan.android.graphy.items.SGraphyLineItem
 import com.sybotan.android.graphy.items.SGraphyRectItem
 import com.sybotan.android.graphy.utils.MatrixTools
+import com.sybotan.base.extensions.toJson
 
 /**
  * SGraphy图形引擎场景类
@@ -66,7 +67,7 @@ open class SGraphyScene {
      * @param   rect        更新绘制区域
      */
     open fun drawScene(canvas: Canvas, rect: RectF) {
-        rootNode.onDraw(canvas, rect)
+        rootNode.onPaint(canvas, rect)
         return
     } // Function paint()
 
@@ -223,6 +224,7 @@ open class SGraphyScene {
      * @return  如果事件被处理返回 true , 否则返回 false 。
      */
     open fun onSingleTapUp(e: SMotionEvent): Boolean {
+        Log.e("手势scene",e.toJson())
         if (null != grabItem) {     // 如果当前锁定item不为空
             val ie = toGrabItemMotionEvent(grabItem!!, e)
             return grabItem!!.onSingleTapUp(ie)
@@ -316,7 +318,9 @@ open class SGraphyScene {
             se.matrix.postRotate(this.view!!.rotate)
         }
         se.matrix.postConcat(item.scene2itemMattrix())
-        val matrixTransform = MatrixTools.matrixTransform(se.matrix, se.x, se.y)
+        val matrix = Matrix()
+        se.matrix.invert(matrix)
+        val matrixTransform = MatrixTools.matrixTransform(matrix, se.x, se.y)
         se.x = matrixTransform.x
         se.y = matrixTransform.y
 //        val p = item.mapFromScene(e.x, e.y)

+ 12 - 1
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/SGraphyView.kt

@@ -33,6 +33,7 @@ import com.sybotan.android.graphy.enums.SGraphyViewTouchState
 import com.sybotan.android.graphy.events.SGraphyViewMoveEvent
 import com.sybotan.android.graphy.events.SGraphyViewZoomEvent
 import com.sybotan.android.graphy.utils.MatrixTools
+import com.sybotan.base.extensions.toJson
 import org.greenrobot.eventbus.EventBus
 import kotlin.math.max
 import kotlin.math.min
@@ -331,6 +332,7 @@ open class SGraphyView(context: Context, attrs: AttributeSet? = null)
      * @return  如果事件被处理返回 true , 否则返回 false 。
      */
     override fun onSingleTapUp(e: MotionEvent): Boolean {
+        Log.e("手势view",e.toString())
         scene?.onSingleTapUp(toSceneMotionEvent(e))
         return true
     } // Function onSingleTapUp()
@@ -368,6 +370,8 @@ open class SGraphyView(context: Context, attrs: AttributeSet? = null)
             // 移动场景
             pos.x = pos.x - distanceX
             pos.y = pos.y - distanceY
+            Log.e("x",pos.x.toString())
+            Log.e("y",pos.y.toString())
             EventBus.getDefault().post(SGraphyViewMoveEvent(this, pos.x, pos.y))
         }
 
@@ -542,12 +546,19 @@ open class SGraphyView(context: Context, attrs: AttributeSet? = null)
      * @return  子对象MotionEvent
      */
     private fun toSceneMotionEvent(e: MotionEvent): SMotionEvent {
+        val src1 = kotlin.floatArrayOf(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f)
+        val src2 = kotlin.floatArrayOf(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f)
         val se = SMotionEvent(e)
         se.matrix.postTranslate(pos.x,pos.y)
         se.matrix.postScale(this.scale, this.scale);
         se.matrix.postRotate(this.rotate)
+        se.matrix.getValues(src1)
+        Log.e("src1= ",src1.toJson())
+
         val matrixMat = Matrix()
-        matrixMat.invert(se.matrix)
+        se.matrix.invert(matrixMat)
+        matrixMat.getValues(src2)
+        Log.e("src2= ",src2.toJson())
         val matrixTransform = MatrixTools.matrixTransform(matrixMat, e.x, e.y)
         se.x = matrixTransform.x
         se.y = matrixTransform.y

+ 18 - 5
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/SMotionEvent.kt

@@ -4,7 +4,11 @@ package com.sybotan.android.graphy
 import android.graphics.Matrix
 import android.view.MotionEvent
 
-
+/**
+ * 自定义处理事件
+ *
+ * @author wx <zhangweixin@sagacloud.cn>
+ */
 class SMotionEvent() {
 
     /** x 坐标 */
@@ -20,20 +24,29 @@ class SMotionEvent() {
     /** 矩阵 */
     var matrix = Matrix()
 
-
-    constructor(e: MotionEvent){
+    /**
+     * 构造器
+     *
+     * @param   e   事件
+     */
+    constructor(e: MotionEvent) : this() {
         this.viewX = e.x
         this.viewY = e.y
         this.action = e.action
     }
 
-    constructor(e: SMotionEvent){
+    /**
+     * 构造器
+     *
+     * @param   e   事件
+     */
+    constructor(e: SMotionEvent) : this() {
         this.x = e.x
         this.y = e.y
         this.viewX = e.viewX
         this.viewY = e.viewY
         this.action = e.action
         matrix.set(e.matrix)
-
     }
+
 }

+ 1 - 1
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/items/SGraphyClockItem.kt

@@ -93,7 +93,7 @@ class SGraphyClockItem(var size: SizeF, parent: SGraphyItem? = null) : SGraphyIt
      * @param   canvas      画布
      * @param   rect        更新区域
      */
-    override fun onDraw(canvas: Canvas, rect: RectF) {
+    override fun onDraw(canvas: Canvas) {
         // 定位坐标原点为控件的中心
         canvas.translate(size.width / 2f, size.height / 2f)
 

+ 1 - 1
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/items/SGraphyImageItem.kt

@@ -63,7 +63,7 @@ open class SGraphyImageItem(var image: Bitmap, parent: SGraphyItem? = null) : SG
      * @param   canvas      画布对象
      * @param   rect        更新区域
      */
-    override fun onDraw(canvas: Canvas, rect: RectF) {
+    override fun onDraw(canvas: Canvas) {
         canvas.drawBitmap(image, 0f, 0f, pen)
         return
     } // Function paint()

+ 1 - 1
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/items/SGraphyLineItem.kt

@@ -79,7 +79,7 @@ open class SGraphyLineItem(line: SLineF = SLineF(), parent: SGraphyItem? = null)
      * @param   canvas      画布对象
      * @param   rect        更新区域
      */
-    override fun onDraw(canvas: Canvas, rect: RectF) {
+    override fun onDraw(canvas: Canvas) {
         canvas.drawLine(line.x1, line.y1, line.x2, line.y2, pen)
         return
     } // Function paint()

+ 1 - 4
sybotan-android-graphy/src/main/java/com/sybotan/android/graphy/items/SGraphyRectItem.kt

@@ -86,16 +86,13 @@ open class SGraphyRectItem(rect: RectF, parent: SGraphyItem? = null) : SGraphyIt
      * @param   canvas      画布对象
      * @param   rect        更新区域
      */
-    override fun onDraw(canvas: Canvas, rect: RectF) {
+    override fun onDraw(canvas: Canvas) {
         pen.color = if (this.isSelected) {
             Color.RED
         } else {
             Color.BLUE
         }
-
         canvas.drawRect(rect, pen)
-
-        super.onDraw(canvas, rect)
         return
     } // Function paint()
 } // Class SGraphyRectItem()