Basic2DGraphicsinAndroid

上传人:lizhe****0001 文档编号:54638432 上传时间:2018-09-16 格式:PPT 页数:17 大小:393.50KB
返回 下载 相关 举报
Basic2DGraphicsinAndroid_第1页
第1页 / 共17页
Basic2DGraphicsinAndroid_第2页
第2页 / 共17页
Basic2DGraphicsinAndroid_第3页
第3页 / 共17页
Basic2DGraphicsinAndroid_第4页
第4页 / 共17页
Basic2DGraphicsinAndroid_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《Basic2DGraphicsinAndroid》由会员分享,可在线阅读,更多相关《Basic2DGraphicsinAndroid(17页珍藏版)》请在金锄头文库上搜索。

1、Basic 2D Graphics in Android,Android Graphics Programming,There are many ways to do graphics programming in Android 2D vs. 3D static vs. dynamicMany of them require a lot of knowledge of the underlying graphics librariesWe will look at the very simplest form of 2D graphics,Drawing on a Canvas,Visibl

2、e elements in an Android UI are called ViewsEach View has an associated Canvas When the View is shown, its onDraw method is automatically called by Android It uses the Canvas to render the different things it wants to displayWe can create our own View with our own onDraw method to display basic obje

3、cts using the Canvas,Canvas and Paint,Canvas has methods for drawing Arcs, Bitmaps, Circles, Lines, Ovals, Paths, Rectangles, etc. Also methods to rotate, scale, skew, translatePaint has methods for setting the alpha, color, shade, stroke, etc.,Lets Create a New Project!,In Eclipse, go to File New P

4、roject Then select “Android Project”Name the project “FunWithDrawing” Specify the package as “edu.upenn.cs4hs” Name the Activity class “FunWithDrawingActivity”Next, create your own custom View class,Creating Your Own View Class,Create a new Java class that extends ViewImplement the necessary constru

5、ctorsImplement the onDraw method and use the Canvas parameter to draw using a Paint objectAdd your View to the applications Layout,1 package edu.upenn.cs4hs;2 3 public class DrawableView extends View 45 / Second, you must implement these constructors!6 public DrawableView(Context c) 7 super(c);8 9 p

6、ublic DrawableView(Context c, AttributeSet a) 10 super(c, a); 11 12 . continued on next slide .,Create a DrawableView class,Still in the DrawableView class.13 / Third, implement the onDraw method. 14 / This method is called when the View is displayed 15 protected void onDraw(Canvas canvas) 16 17 / t

7、his is the “paintbrush” 18 Paint paint = new Paint(); 19 / set the color 20 paint.setColor(Color.RED); 21 22 / draw Rectangle with corners at (40, 20) and (90, 80) 23 canvas.drawRect(40, 20, 90, 80, paint); 24 25 / change the color 26 paint.setColor(Color.BLUE); 27 / set a shadow 28 paint.setShadowL

8、ayer(10, 10, 10, Color.GREEN); 29 30 / create a “bounding rectangle” 31 RectF rect = new RectF(150, 150, 280, 280); 32 / draw an oval in the bounding rectangle 33 canvas.drawOval(rect, paint); 34 35 36 / end of DrawableView class,1 2 3 8,Modify main.xml as follows,Detecting User Interaction and Touc

9、h Events,Detecting Touch Events,When the user touches/clicks on the View, Android invokes the Views onTouchEvent methodA MotionEvent object is automatically generated and is passed to the methodFrom the MotionEvent, you can determine: the type of Action (down, up/release, move) where the event occur

10、red (x/y coordinate) the time at which the event occurred,Modifying the DrawableView,In your DrawableView class, modify onDraw so that the color of the rectangle is randomizedThen add an onTouchEvent method that looks for an “up” action and calls this.invalidate if the touch is within the bounds of

11、the rectangle,1 package edu.upenn.cs4hs;2 3 public class DrawableView extends View 45 / these constructors shouldnt change6 public DrawableView(Context c) 7 super(c);8 9 public DrawableView(Context c, AttributeSet a) 10 super(c, a); 11 12,DrawableView class,13 / This version of onDraw randomly choos

12、es a color 14 / to use when drawing the rectangle 15 protected void onDraw(Canvas canvas) 16 17 / this is the “paintbrush” 18 Paint paint = new Paint(); 19 20 / set the color randomly 21 int whichColor = (int)(Math.random() * 4); 22 if (whichColor = 0) paint.setColor(Color.RED); 23 else if (whichCol

13、or = 1) paint.setColor(Color.GREEN); 24 else if (whichColor = 2) paint.setColor(Color.BLUE); 25 else paint.setColor(Color.YELLOW); 26 27 / draw Rectangle with corners at (40, 20) and (90, 80) 28 canvas.drawRect(40, 20, 90, 80, paint); 29 30 31 ,Modify onDraw as follows,32 / this method is called whe

14、n the user touches the View 33 public boolean onTouchEvent(MotionEvent event) 34 35 / if its an up (“release”) action 36 if (event.getAction() = MotionEvent.ACTION_UP) 37 38 / get the coordinates 39 float x = event.getX(); 40 float y = event.getY(); 41 42 / see if they clicked on the box 43 if (x =

15、40 51 / end of onTouchEvent 52 / end of DrawableView class,Add an onTouchEvent method,Helpful Hint: Debugging,Log.v(tag, message); System.out.println(message); Window Show View Other. Android LogCatTo show a pop-up window: Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();,Coming up after lunch,Creating and handling formsThreadsSystem servicesDiscussion: how can you use Android in your high school CS course?,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号