android 矩阵坐标转换,使用矩阵旋转后,获得坐标的新位置_android_开发99编程知识库...

该博客展示了一个Android矩阵坐标转换的简单演示。创建了main.xml布局和RotationActivity类,通过SeekBar控制旋转角度,利用Matrix进行坐标转换,在自定义的MyDrawing类中实现绘制矩形和点,并在旋转时更新坐标,最后在onDraw方法中绘制元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我已經創建了一個簡單的演示。 它有一點額外的,所以你也可以看到如何在繪圖中使用它。

main.xml<?xml version="1.0" encoding="utf-8"?>

android:id="@+id/container"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/seekBar1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"/>

和 Activity:package nl.entreco.android.testrotation;

import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.graphics.Point;

import android.graphics.Rect;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.RelativeLayout;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

public class RotationActivity extends Activity implements OnSeekBarChangeListener {

private MyDrawing myDrawing;

private SeekBar mSeekbar;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Rect rect = new Rect(150,150,440,630);

int x = (int) (rect.left + Math.random() * rect.width());

int y = (int) (rect.top + Math.random() * rect.height());

Point coordinate = new Point(x, y);

//To draw the rect we create a CustomView

myDrawing = new MyDrawing(this, rect, coordinate);

RelativeLayout rl = (RelativeLayout)findViewById(R.id.container);

rl.addView(myDrawing);

mSeekbar = (SeekBar)findViewById(R.id.seekBar1);

mSeekbar.setMax(360);

mSeekbar.setOnSeekBarChangeListener(this);

}

private class MyDrawing extends View

{

private Rect myRect;

private Point myPoint;

private Paint rectPaint;

private Paint pointPaint;

private Matrix transform;

public MyDrawing(Context context, Rect rect, Point point)

{

super(context);

//Store the Rect and Point

myRect = rect;

myPoint = point;

//Create Paint so we can see something :)

rectPaint = new Paint();

rectPaint.setColor(Color.GREEN);

pointPaint = new Paint();

pointPaint.setColor(Color.YELLOW);

//Create a matrix to do rotation

transform = new Matrix();

}

/**

* Add the Rotation to our Transform matrix.

*

* A new point, with the rotated coordinates will be returned

* @param degrees

* @return

*/

public Point rotate(float degrees)

{

//This is to rotate about the Rectangles center

transform.setRotate(degrees, myRect.exactCenterX(), myRect.exactCenterY());

//Create new float[] to hold the rotated coordinates

float[] pts = new float[2];

//Initialize the array with our Coordinate

pts[0] = myPoint.x;

pts[1] = myPoint.y;

//Use the Matrix to map the points

transform.mapPoints(pts);

//NOTE: pts will be changed by transform.mapPoints call

//after the call, pts will hold the new cooridnates

//Now, create a new Point from our new coordinates

Point newPoint = new Point((int)pts[0], (int)pts[1]);

//Return the new point

return newPoint;

}

@Override

public void onDraw(Canvas canvas)

{

if(myRect!= null && myPoint!= null)

{

//This is an easy way to apply the same transformation (e.g. rotation)

//To the complete canvas.

canvas.setMatrix(transform);

//With the Canvas being rotated, we can simply draw

//All our elements (Rect and Point)

canvas.drawRect(myRect, rectPaint);

canvas.drawCircle(myPoint.x, myPoint.y, 5, pointPaint);

}

}

}

@Override

public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {

Point newCoordinates = myDrawing.rotate(progress);

//Now -> our float[] pts contains the new x,y coordinates

Log.d("test","Before Rotate myPoint("+newCoordinates.x+","+newCoordinates.y+")");

myDrawing.invalidate();

}

@Override

public void onStartTrackingTouch(SeekBar seekBar) {}

@Override

public void onStopTrackingTouch(SeekBar seekBar) {}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值