Android Studio里面的在canvas = sh.lockcanvas 返回null();返回为空,怎么解决,有请高手!

1286人阅读
android(121)
butterknife
Building Project Ant
What you should know before starting
AndroidAnnotations works by generating code at&compile time.
AndroidAnnotations provides two&jars:
androidannotations-X.Y.jar&is needed to generate the code at compile time. There is no reason to keep this&jar&at&runtime&because:
Its code will never be executed at runtime.It makes your&APK&size bigger than needed.
androidannotations-X.Y-api.jar&only contains the code you need at runtime.
Prerequisites
This tutorial is based on the SDK v19. If you use another version, you may need to adapt this tutorial.
if you don't already have a&build.xml&file, you can easily generate one:
android update project --path &$PROJECT_ROOT$&
Create a new folder at the root of your project (compile-libs&would be a good candidate)
and put&androidannotations-X.Y.jar&in this folder.
Put&androidannotations-X.Y-api.jar&in the&$PROJECT_ROOT$/libs
Create a&custom_rules.xml&Ant file next to your&build.xml&Ant
script. Thebuild.xml&script generated by the Android tools automatically importscustom_rules.xml&if
it exists. This enables you to customize the build, without having to modify&build.xml, which can therefore be easily updated.
Add properties for the generated source folder in&custom_rules.xml
&property name=&generated.dir& value=&.apt_generated& /&
&property name=&generated.absolute.dir& location=&${generated.dir}& /&
&property name=&</pilerargs& value=&-s &${generated.absolute.dir}&& /&
Note:&In some case you may have to replace&.apt_generated&by&gen&to
make the whole thing works.
Override the&-pre-compile&target in&custom_rules.xml
&target name=&-pre-compile&&
&mkdir dir=&${generated.absolute.dir}& /&
Override the&-compile&target in&custom_rules.xml
Open&$ANDROID_SDK_ROOT$/tools/ant/build.xml
Locate the&-compile&target in this file:
&target name=&-compile& depends=&-build-setup, -pre-build, -code-gen, -pre-compile&&
Copy the target and its content into&custom_rules.xml
Modify the classpath when&javac&is invoked by adding a&&fileset&&node,
and configure javac to generates the sources in a dedicated folder:
&target name=&-compile& ...&
&path id=&project.javac.classpath&&
&fileset dir=&compile-libs& includes=&*.jar&/&
You should now be able to build you project using ant:
ant clean release
Configure&&or&&AndroidAnnotations
If you put the two AndroidAnnotations jars in the&$PROJECT_ROOT$/libsyou will encounter the following error:
java.lang.IllegalArgumentException: already added: Lcom/googlecode/androidannotations/annotations/AfterV
androidannotations-X.Y-api.jar&is a subset of&androidannotations-X.Y.jar. So each class in&androidannotations-X.Y-api.jar&is present in&androidannotations-X.Y.jar.
This error is thrown when the dx command is invoked and two classes with the same name and package name are detected. To prevent this error you have to move&androidannotations-X.Y.jar&file away from the&$PROJECT_ROOT$/libsfolder.
Anything else? Contact us on the&, or&, and we'll try to help you.
After upgrading the Android SDK, the content of$ANDROID_SDK_ROOT$/tools/ant/build.xml&may have changed. Therefore, your ant build may be broken. The solution is to replace the content of the&-compile&target
in&custom_rules.xml&with the new&-compile&target
content defined in$ANDROID_SDK_ROOT$/tools/ant/build.xml
在实际操作中
&fileset dir=&compile-libs& includes=&*.jar&/&
这句是删掉的
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:233771次
积分:3430
积分:3430
排名:第7181名
原创:115篇
转载:42篇
评论:66条
(3)(1)(2)(5)(2)(5)(8)(3)(2)(1)(4)(2)(3)(5)(9)(8)(10)(15)(14)(1)(6)(17)(8)(16)(7)(1)Canvas - WEB前端 - 伯乐在线Android canvas的save()和restore()用法_android开发_ThinkSAAS
Android canvas的save()和restore()用法
Android canvas的save()和restore()用法
在读Android SDK里面的samples里面游戏的时候,很多时候都看到对canvas的save()和restore()运用。
下面是个小程序,使用了这两个方面,使得旋转红色方块的时候,保证蓝色方块不受影响。
代码如下:
package com.ray.
import android.app.A
import android.content.C
import android.graphics.C
import android.graphics.C
import android.graphics.P
import android.os.B
import android.view.SurfaceH
import android.view.SurfaceV
public class Test extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
class MyView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mH
public MyView(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
public void surfaceCreated(SurfaceHolder holder) {
canvas = mHolder.lockCanvas();
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);
canvas.drawRect(100, 200, 200, 300, mPaint);
canvas.save();
canvas.rotate(45);
mPaint.setColor(Color.RED);
canvas.drawRect(150, 10, 200, 60, mPaint);
canvas.restore();
mHolder.unlockCanvasAndPost(canvas);
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
下载次数: 598
浏览 13485
论坛回复 /
(7 / 12377)
但是貌似有个问题是,旋转canvas和直接旋转对象两个比起来,旋转画布所得到的质量似乎要低一些……
这个可以通过设置画笔的抗锯齿来抵消, 不过有的图片即使抗锯齿了也不行
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信1108人阅读
Android(40)
画图需要四大基本要素:
1、一个用来保存像素的Bitmap;
2、一个Canvas画布,绘制Bitmap操作;
3、绘制的东西
4、绘制的画笔Paint(颜色和样式)
如何获得一个Canvas对象?
通过重写View.onDraw方法自己创建一个Canvas对象调用SurfaceHolder.lockCanvas(),返回一个Canvas 在 surfaceView 或 TextureView中使用
自己创建一个Canvas 方法如下:
//得到一个Bitmap对象,当然也可以使用别的方式得到。
//但是要注意,该bitmap一定要是mutable(异变的)
Bitmap b = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
/*先new一个Canvas对象,在调用setBitmap方法,一样的效果
* Canvas c = new Canvas();
* c.setBitmap(b);
canvas的常用绘制方法:
& & & drawARGB(int a, int r, int g, int b)
& & & drawColor(int color)
& & & drawRGB(int r, int g, int b)
& & & drawColor(int color, PorterDuff.Mode mode)
2. 几何图形
& & &canvas.drawArc (扇形)
& & &canvas.drawCircle(圆)
& & &canvas.drawOval(椭圆)
& & &canvas.drawLine(线)
& & &canvas.drawPoint(点)
& & &canvas.drawRect(矩形)
& & &canvas.drawRoundRect(圆角矩形)
& & &canvas.drawVertices(顶点)
& & &cnavas.drawPath(路径)
& & & &canvas.drawBitmap (位图)
& & & &canvas.drawPicture (图片)
& & & &canvas.drawText
Canvas的保存和回滚
Canvas还提供了保存和回滚属性的方法(save和restore),
比如你可以先保存目前画纸的位置(save),然后旋转90度,向下移动100像素后画一些图形,画完后调用restore方法返回到刚才保存的位置。
相关方法的介绍如下:&
* 保存当前的matrix和clip到私有的栈中(Skia内部实现)。任何matrix变换和clip操作都会在调用restore的时候还原。
* @return 返回值可以传入到restoreToCount()方法,以返回到某个save状态之前。
public native int save();
* 传入一个标志,来表示当restore 的时候,哪些参数需要还原。该参数定义在Canvas中,参照下面。
* save()方法默认的是还原matrix和clip,但是可以使用这个方法指定哪些需要还原。并且只有指定matrix和clip才有效,其余的几个参数是
* 用于saveLayer()和saveLayerAlpha()方法 的。
public native int save(int saveFlags);
* 回到上一个save调用之前的状态,如果restore调用的次数大于save方法,会出错。
public native void restore();
* 返回栈中保存的状态,值等译 save()调用次数-restore()调用次数
public native int getSaveCount();
* 回到任何一个save()方法调用之前的状态
public native void restoreToCount(int saveCount);
* saveFlags的参数
public static final int MATRIX_SAVE_FLAG = 0x01;//需要还原Matrix
public static final int CLIP_SAVE_FLAG = 0x02;//需要还原Clip
public static final int HAS_ALPHA_LAYER_SAVE_FLAG = 0x04;// 图层的 clip 标记,
public static final int FULL_COLOR_LAYER_SAVE_FLAG = 0x08;// 图层的 color 标记,
public static final int CLIP_TO_LAYER_SAVE_FLAG = 0x10;// 图层的 clip 标记,在saveLayer 和 saveLayerAlpha Android强烈建议必须加上他
public static final int ALL_SAVE_FLAG = 0x1F; //还原所有 一般情况都是使用这个
/*关于saveLayer的具体flags还不大明白它的含义,具体怎么使用在下面例子中*/
public int saveLayer(RectF bounds, Paint paint, int saveFlags)
public int saveLayer(float left, float top, float right, float bottom,Paint paint, int saveFlags)
public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags)
public int saveLayerAlpha(float left, float top, float right, float bottom,int alpha, int saveFlags)
上图是从网上扣下来的, &我一开始看的还是有点迷糊混乱的,下面做下解释:
上图 最初始 的情况 是栈里 只有 0 1 2 3 4 , &
然后 执行 &save 方法 两次, &则 5 和6 出现在栈中
第一次调用 save 方法的时候 返回的是 4, 这时 &5 压入栈
第二次调用 save 方法的时候 返回的是 6, 这时 &6 压入栈
save返回的 之前的栈的 数目
如果 调用 &restoreToCount(5) 那么就返回到5 的状态了, 即 &即调用 第二次 save之前的状态
如果调用 两次 restore 方法, 那么会一次弹出 6 和 5 &最后回到 4 的状态
Canvas 在一般的情况下可以看作是一张画布,所有的绘图操作如drawBitmap, drawCircle都发生在这张画布上,这张画板还定义了一些属性比如Matrix,颜色等等
但是如果需要实现一些相对复杂的绘图操作,比如多层动画,地图(地图可以有多个地图层叠加而成,比如:政区层,道路层,兴趣点层)。
Canvas提供了图层(Layer)支持,缺省情况可以看作是只有一个图层Layer。
如果需要按层次来绘图,Android的Canvas可以使用SaveLayerXXX, Restore 来创建一些中间层,
对于这些Layer是按照“栈结构“来管理的: &&
创建一个新的Layer到“栈”中,可以使用saveLayer, savaLayerAlpha,
从“栈”中推出一个Layer,可以使用restore,restoreToCount
但Layer入栈时,后续的DrawXXX操作都发生在这个Layer上,
而Layer退栈时,就会把本层绘制的图像“绘制”到上层或是Canvas(即最底层)上,
在绘制到上层或是Canvas(即最底层)上时,可以指定Layer的透明度(Layer)
这是在创建Layer时指定的:public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags)
本例Layers 介绍了图层的基本用法:
Canvas可以看做是由两个图层(Layer)构成的,为了更好的说明问题,
我们将代码稍微修改一下,缺省图层绘制一个红色的圆,
在新的图层画一个蓝色的圆,新图层的透明度为0×88。
public class LayersTestView extends View {
public static final String TAG = &LayersTestView&;
private Paint mP
public LayersTestView(Context context) {
super(context);
public LayersTestView(Context context, AttributeSet attrs) {
super(context, attrs);
public LayersTestView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
mPaint.setColor(Color.RED);
canvas.drawCircle(75, 75, 75, mPaint);
canvas.translate(25, 25);
LogUtil.d(TAG, &getCounet2 = & + canvas.getSaveCount());
int count = canvas.saveLayerAlpha(0, 0, 200, 200, 0x88, Canvas.ALL_SAVE_FLAG);
LogUtil.d(TAG, &count = & + count + & , getCounet2 = & + canvas.getSaveCount());
mPaint.setColor(Color.BLUE);
canvas.drawCircle(125, 125, 75, mPaint);
canvas.restore();
canvas.drawCircle(30, 30, 30, mPaint);
LogUtil.d(TAG, &getCounet3 = & + canvas.getSaveCount());
一开始 在 最底层的默认 &图层上画一个 红色圆.
然后再&canvas.saveLayerAlpha 新建一个 不透明度为 0x88的图层, 在上面划了了一个 蓝色的圆 这个图层是有透明度, 我们在手机上看到的是一个带有透明度的 圆
然后在&canvas.restore 后,再花了一个 圆, 这个圆 是在原来的图层上划的, 没有透明度 如图:&
上面需要注意的是
我调用的canvas.translate(10, 10); 即把 画布平移 对 新建的那个图层是依然生效的.我们调用&saveLayerAlpha方法, 也会和 save 方法一样, 不仅在 layer 的那个栈中压入一个图层, 同时在还会在 上面 说 save 和 restore 那里压入一个栈.
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:61052次
积分:1244
积分:1244
排名:千里之外
原创:66篇
评论:16条
(3)(2)(2)(20)(15)(6)(2)(1)(1)(3)(5)(11)}

我要回帖

更多关于 studio.sh 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信