我输入了一个在 Canvas 上绘制的程序。
它提供了一个弹出菜单,其中提供了 3 个绘图工具作为选项:
边划线边划线
根据屏幕上的起点和终点画线
画一个圆
此外,还有如下选项:
清除
撤消
在行上执行撤消时,完全没有问题,因为两者都是基于路径的。 (使用 List<Path> )。
但是这里开始了问题。圆是使用 Point 对象绘制的。所以问题是:
下面共享的代码(很复杂)。我试图为每个绘图工具(线、圆)专门设置一个类——它起作用了——除了——它没有在 Canvas 上画任何东西。所以,全部打包回1类。
代码:
package com.example.orbit_.undofortouch;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
Button b1, b2, b3;
PopupMenu popup;
int dtool;
boolean touch,circle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout2);
final DrawPanel dp = new DrawPanel(this);
linearLayout.addView(dp);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dp.Clear();
}
});
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dp.Undo();
}
});
b3 = (Button) findViewById(R.id.button3);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup = new PopupMenu(MainActivity.this, v);
popup.getMenuInflater().inflate(R.menu.menu_main, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.touch:
dtool = 1;
break;
case R.id.line:
dtool = 2;
break;
case R.id.circle:
dtool = 3;
break;
}
Log.v("EDITL:", "Drawtool:".concat(String.valueOf(item.getTitle())));
Toast.makeText(MainActivity.this,"Clicked popup menu item " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}
class DrawPanel extends View implements View.OnTouchListener {
Bitmap bmp;
Canvas canvas;
List<Path> paths, undone;
List<Point> circlePoints,removeCircles;
Paint paint;
Path path;
Point point;
public DrawPanel(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
}
public DrawPanel(Context context) {
super(context);
paint = new Paint();
path = new Path();
paths = new ArrayList<>();
undone = new ArrayList<>();
circlePoints = new ArrayList<>();
removeCircles = new ArrayList<>();
canvas = new Canvas();
this.setOnTouchListener(this);
paint.setStrokeWidth(3);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setDither(true);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.desert);
touch=false;
circle=false;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
for (Path p : paths)
canvas.drawPath(p, paint);
if (touch)
canvas.drawPath(path, paint);
touch = false;
Log.v("Inside onDraw","Circle is".concat(String.valueOf(circle)));
for (Point p : circlePoints)
canvas.drawCircle(p.x, p.y, 5, paint);
}
float mX, mY,mx,my;
final float TOUCH_TOLERANCE = 0;
private void touch_start(float x, float y) {
undone.clear();
Log.v("ONTOUCH:", "Inside DOWN".concat("DOWN-X---:").concat(String.valueOf(x)).concat("**DOWN-Y---:").concat(String.valueOf(y)));
path.reset();
path.moveTo(x, y);
mX = x;
mY = y;
mx = x;
my = y;
}
private void touch_up() {
paths.add(path);
path = new Path();
}
private void touch_move(float x, float y) {
path.moveTo(mX, mY);
Log.v("ONTOUCH:", "Inside MOVE".concat("mX:").concat(String.valueOf(mX)).concat("mY:").concat(String.valueOf(mY)));
Log.v("ONTOUCH:", "Inside MOVE".concat("MOVE-X---:").concat(String.valueOf(x)).concat("**MOVE-Y---:").concat(String.valueOf(y)));
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
path.lineTo(mX, mY);
Log.v("MOVE:", " PATH ADDED & New Created");
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (dtool) {
case 1:
touch=true;
Touch(v, event, x, y);
break;
case 2:
Line(v,event,x,y);
break;
case 3:
Circle(v,event,x,y);
break;
}
Log.v("ONTOUCH:", "OUTSIDE CASE");
return true;
}
public void Line(View v, MotionEvent event, float x, float y) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
}
}
public void Touch(View v, MotionEvent event, float x, float y) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
canvas.drawPath(path, paint);
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
canvas.drawPath(path, paint);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
}
}
public void Circle(View v, MotionEvent event, float x, float y)
{ point = new Point();
point.x = (int)x;
point.y = (int)y;
path.moveTo(x,y);
circle=true;
if(event.getAction()==MotionEvent.ACTION_DOWN) {
circlePoints.add(new Point(Math.round(point.x), Math.round(point.y)));
invalidate();
Log.v("Circle", "Inside Circle");
circlePoints.add(point);
paths.add(path);
}
}
public void Clear() {
paths.clear(); //Needs to be experimented
path.reset();
invalidate();
}
public void Undo() {
if (paths.size() > 0) {
undone.add(paths.remove(paths.size() - 1));
invalidate();
}
else if(circlePoints.size()>0)
{
removeCircles.add(circlePoints.remove(circlePoints.size()-1));
invalidate();
}
}
}
}
XML 布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0"
android:layout_gravity="top">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Undo"
android:id="@+id/button2"
android:layout_gravity="right"
android:layout_marginTop="-50dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_gravity="center_vertical|bottom"
android:layout_marginTop="-50dp"
android:enabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tools"
android:id="@+id/button3"
android:layout_gravity="center_horizontal"
android:layout_weight="0"
android:layout_marginTop="-50dp" />
</LinearLayout>
XML 主菜单代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/touch"
android:title="Touch"/>
<item
android:id="@+id/circle"
android:title="Circle"/>
<item
android:id="@+id/line"
android:title="Line"/>
</menu>
最佳答案
Path.addCircle(float x,float y,float radius, Path.Direction)
这个简单的代码完成了这件事。因为添加圆的点将包含在 Path 对象中。 paths.addPath(path) 只是将其添加到之前的路径列表(绘制的线条)中。
因此撤消也变得简单自然。因此,解决方案。
感谢@pskink 提供原始解决方案。
P.S:今天我意识到,从一个未完成的项目中休息并不是一个好习惯,但在某种程度上有时对某些人来说是这样,因为你不熟悉了,现在可以用以前不能的正常方式思考.
关于android - 我们可以画一个使用 Path 对象的圆吗? [在参数中,如 drawPath()],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32518421/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t