标签: android-canvas

Android - 捏缩放ontouch事件坐标

我正在尝试获取我正在创建的Android应用程序的画布坐标.它很有用,直到我添加代码以使用缩放焦点(以下两行):

scalePoint.setX((int) detector.getFocusX());
scalePoint.setY((int) detector.getFocusY());
Run Code Online (Sandbox Code Playgroud)

这是我的视图类的源代码:

    package us.kniffin.Jigsaw;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class TestView extends View {
    private float mLastTouchX;
    private float mLastTouchY;
    private float mPosX;
    private float mPosY;

    private Rect rect;

    private float cX, cY; // circle coords


    // Scaling objects
    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;
    // The focus point for the scaling
    private float scalePointX; 
    private …
Run Code Online (Sandbox Code Playgroud)

android scale pan android-canvas

4
推荐指数
1
解决办法
7803
查看次数

如何在Android中旋转画布上绘制的矩形?

我正在使用下面的代码在android画布上绘制文本

        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        canvas.translate(xPosition + position.getX(), yPosition + position.getY());
        paint.setColor(Color.BLUE);
        paint.setStyle(Style.STROKE);
        canvas.drawRect(rect, paint);
        paint.setStyle(Style.FILL);
        paint.setColor(text_color);
        canvas.translate(-(xPosition + position.getX()), -(yPosition + position.getY()));
        canvas.rotate(getDegreesFromRadians(angle), xPosition + position.getX() + rect.exactCenterX(), yPosition + position.getY() + rect.exactCenterY());
        canvas.drawText(text, xPosition + position.getX(), yPosition + position.getY(), paint);
Run Code Online (Sandbox Code Playgroud)

此代码负责文本的旋转,并且工作正常.我正在使用上面的代码在文本周围绘制一个蓝色矩形.现在我的问题是矩形不随文本一起旋转.它仍然保持不变.有没有办法旋转Android画布中绘制的矩形?

android rotation rect draw android-canvas

4
推荐指数
1
解决办法
1万
查看次数

DrawText只在Jelly Bean 4.2上绘制一个字符串字符

好的,这让我疯了.我正在使用画布在我的应用程序中绘制一个仪表.它还会在散列标记上绘制数字,并在整个标尺上绘制一些水平文本.这一切都适用于所有版本的Android,最高可达4.2.在4.2中,它只从它应该写的文本中绘制一个字符(它看起来是中间字符).例如,如果我的文本读取为12345,则只写入3.如果是两位数,则仅绘制第一个数字.我在4.2模拟器和带有4.2.1的Nexus 4上看到了这种行为.我阅读了有关默认情况下打开的硬件加速的所有内容,并且它会导致某些Paint和Canvas功能出现问题.我已插入代码以关闭drawText调用的硬件加速,但它没有帮助.

这是我的油漆代码:

titlePaint1 = new Paint();
titlePaint1.setColor(Color.WHITE);
titlePaint1.setAntiAlias(true);
titlePaint1.setTypeface(Typeface.DEFAULT_BOLD);
titlePaint1.setTextAlign(Paint.Align.CENTER);
titlePaint1.setTextSize(0.085f);
Run Code Online (Sandbox Code Playgroud)

这是调用绘制文本的方法:

@SuppressLint("NewApi")
private void drawTitle1(Canvas canvas) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        setLayerType(View.LAYER_TYPE_SOFTWARE, titlePaint1);

    canvas.drawText(title1, 0.5f, 0.72f, titlePaint1);
}
Run Code Online (Sandbox Code Playgroud)

我把它传递给有效的画布,宽度和高度都很完美.在画布上绘制的所有其他内容都显示正常,并且在绘制测量仪的组件之前,还有其他三个函数调用,如上面的函数.这只是drawText的一个问题.我知道这与4.2中的更改有关,我不认为这个drawText问题与硬件加速有关.我只能在谷歌上找到一些关于4.2的Canvas问题的结果,但没有任何帮助我解决我的问题.任何想法我如何解决这个问题并使文本正确显示?

android android-canvas android-4.2-jelly-bean

4
推荐指数
1
解决办法
2314
查看次数

在android中绘制一个箭头

我试图画一个箭头指向我图像中的对象.我已经能够编写代码来绘制线但是似乎无法找到绘制箭头的方法.我写的用于绘制可拖动线的代码如下.我需要在ACTION_UP事件上绘制一个箭头到线指向的方向

if(event.getAction() ==MotionEvent.ACTION_DOWN) {         
     if (count==1){ 
          x1 = event.getX();
          y1 = event.getY();
          System.out.println(count+"count of value a;skd");
          Toast.makeText(getApplicationContext(), ""+(radius+count), Toast.LENGTH_LONG).show();

          Log.i(TAG, "coordinate x1 : "+String.valueOf(x1)+" y1 : "+String.valueOf(y1));
     }
}
else if(event.getAction() ==MotionEvent.ACTION_MOVE){

     imageView.setImageBitmap(bmp2);
     x2 = event.getX();
     y2 = event.getY();
     posX=(float)(x1+x2)/2;
     posY=(float)(y1+y2)/2;
     radius=(float) Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))/2;
     onDraw();
     Toast.makeText(getApplicationContext(), ""+radius, Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)

嗨,对于任何仍然需要帮助的人.这就是我在最后的方式做到了浮动h =(浮动)30.0;

    float phi = (float) Math.atan2(y2 - y1, x2 - x1); 
    float angle1 = (float) (phi - Math.PI / 6); 
    float angle2 = (float) (phi + Math.PI / 6); …
Run Code Online (Sandbox Code Playgroud)

android android-imageview android-canvas

4
推荐指数
1
解决办法
2200
查看次数

Android - 如何旋转Rect对象?

我有一个矩形:Rect r = new Rect();.我想将r对象旋转到45度.我检查了解决方案,我发现它可以用矩阵完成:

Matrix m = new Matrix();
// point is the point about which to rotate.
m.setRotate(degrees, point.x, point.y);
m.mapRect(r);
Run Code Online (Sandbox Code Playgroud)

问题是乳清我传递rm.mapRect(r);它抱怨r应该是从类型RectF.我成功地做到了:

RectF r2 = new RectF(r);
Matrix m = new Matrix();
// point is the point about which to rotate.
m.setRotate(degrees, point.x, point.y);
m.mapRect(r2);
Run Code Online (Sandbox Code Playgroud)

但问题是,我需要从类型的对象Rect不是RectF.因为我将r对象传递给正在接受Rect对象的外部类.

是否有另一种方法来旋转矩形r窗体类型,Rect除了这个方法,并且没有旋转整个画布(画布包含一些其他元素)?

先感谢您!

此致,Dimitar Georgiev

java android android-canvas

4
推荐指数
1
解决办法
1万
查看次数

在填充的Android画布上绘制一个透明圆圈

在此输入图像描述

我想做一些非常简单的事情(见上文).我希望画布的所有像素都是纯色,但填充中心圆的像素除外.我已经阅读了关于这个主题的数百个堆栈溢出帖子,并尝试了数百种事情,包括设置PorterDuff.Mode.这是我当前onDraw()的MyView扩展视图:

  protected void onDraw(Canvas canvas) {
    int w = canvas.getWidth();
    int h = canvas.getHeight();

    Paint framePaint = new Paint();
    framePaint.setStyle(Paint.Style.FILL);
    framePaint.setColor(Color.BLUE);
    canvas.drawRect(0, 0, w, h, framePaint);

    Paint transparentPaint = new Paint();
    transparentPaint.setColor(Color.TRANSPARENT);
    transparentPaint.setAntiAlias(true);
    canvas.drawCircle(w / 2, h / 2, (w + h) / 4, transparentPaint);
  }
Run Code Online (Sandbox Code Playgroud)

我误解了什么,为什么我不能用透明涂料涂在现有的像素上.当我这样做时,像素保持不变.当我使用PorterDuff时,像素变黑.请帮忙.

android transparent porter-duff android-canvas

4
推荐指数
1
解决办法
6724
查看次数

在Android中逐渐绘制路径

我有一个自定义视图,我想要绘制一个路径,如边框.但是边界应该逐渐消失,就像蛇的大小一样.目的是将其用作玩家在游戏中移动的计时器.

我使用Path类和方法lineTo和addArc来绘制边框.

timerPath = new Path();
timerPath.moveTo(getTranslationX() + width / 2, getTranslationY() + 3);
timerPath.lineTo(getTranslationX() + width - 10, getTranslationY() + 3);
timerPath.addArc(new RectF(getTranslationX() + width - 20, getTranslationY() + 3, 
getTranslationX() + width - 3, getTranslationY() + 20), -90f, 90f);
...
...
timerPath.lineTo(getTranslationX() + width / 2, getTranslationY() + 3);

timerPaint = new Paint();
timerPaint.setColor(Color.GREEN);
timerPaint.setStyle(Paint.Style.STROKE);
timerPaint.setStrokeWidth(6);
Run Code Online (Sandbox Code Playgroud)

我在onDraw中使用drawPath()方法:

canvas.drawPath(timerPath, timerPaint);
Run Code Online (Sandbox Code Playgroud)

它看起来很好.

现在,我想知道是否有办法使用百分比(10%,11%,12%......等)绘制路径的一部分.然后我就可以为绘图制作动画了.

如果不可能,还有另一种动画边框绘制方式吗?(用作计时器)

感谢您的帮助.

android android-canvas

4
推荐指数
1
解决办法
1152
查看次数

如何在Android中剪辑明星?但明星的外观很明显

首先看下面的图像.

1.明星

2.Painted明星

package com.syncfusion.rating;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.view.View;
/**
 * Created by chozarajan.pandiyarajan on 10/9/2015.
 */

public class SfRatingItem extends View {

private   int fillColor,minDim,topXPoint,topYPoint;
private double bigHypot,bigA,bigB,littleHypot,littleA,littleB,value;
private  Paint starPaint;
private Path path;

public SfRatingItem(Context context) {
    super(context);
    starPaint=new Paint();
    fillPaint=new Paint();
    path = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
    starPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    starPaint.setAntiAlias(true);

    minDim = Math.min(this.getWidth() - this.getPaddingLeft() -this.getPaddingRight(), this.getHeight() - this.getPaddingTop() - this.getPaddingBottom());

    bigHypot = (minDim / …
Run Code Online (Sandbox Code Playgroud)

android clipping path paint android-canvas

4
推荐指数
1
解决办法
3419
查看次数

在Android中用锐角绘制一颗心

以下问题可能听起来有点愚蠢,但我觉得愚蠢没有限制,所以在这里.我在Android中使用Canvas绘制了一个Heart并且在绘制内心方面没有任何问题,但是我无法在会面点让人心旷神怡.我的心看起来像在此输入图像描述

代码:

            left_x_moveto = 200;
            left_y_moveto = 45;

            left_x1 = 197;
            left_y1 = -35;
            left_x2 = 60;
            left_y2 = 20;
            left_x3 = 193;
            left_y3 = 130;

            right_x_moveto = 200;
            right_y_moveto = 45;

            right_x1 = 197;
            right_y1 = -35;
            right_x2 = 345;
            right_y2 = 20;
            right_x3 = 193;
            right_y3 = 130;



           heart_outline_paint.setColor(getResources().getColor(R.color.heart_outline_color)); // Change the boundary color
        heart_outline_paint.setStrokeWidth(15);
        heart_outline_paint.setStyle(Paint.Style.STROKE);

        path.moveTo(left_x_moveto, left_y_moveto);
        path.cubicTo(left_x1, left_y1, left_x2, left_y2, left_x3, left_y3);

        path.moveTo(right_x_moveto, right_y_moveto);
        path.cubicTo(right_x1, right_y1, right_x2, right_y2, right_x3, right_y3);
        canvas.drawPath(path, heart_outline_paint);
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试了什么:

  1. 减少或增加点数left_x_moveto, …

android android-canvas

4
推荐指数
1
解决办法
1607
查看次数

如何在android中设置arc的绘制动画

我是Android的新手,我使用drawArc函数向用户显示某些任务的进度,但现在我想对其进行动画处理,使其看起来好像在增长.

我使用以下代码但不工作:

   new Thread(new Runnable() {
    int i=0;
    float startAngle =0;
    float swipeAngle = 40.7f;
    public void run() {
        while (i < swipeAngle) {
            canvas.drawArc(rectF, startAngle, i, false, paint);

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        i++;
    }
}).start();
Run Code Online (Sandbox Code Playgroud)

有人可以建议这里有什么错误或者可以建议一些其他动画的想法.

animation android android-canvas

4
推荐指数
2
解决办法
7044
查看次数