How to use canvas in your android apps - Part 1
| If you want to use a custom layout with a canvas in your application, this tutorial may be useful to you. We will draw a kangoo at the end of this tutorial. There is a cool article on site Custom View - HorizontalSlider, what you can use to understand the following code: |
|
- [...]
- <com.helloandroid.canvastutorial.Panel android:id="@+id/SurfaceView01"
android:layout_width="wrap_content" - android:layout_height="wrap_content" android:maxHeight="40dip">
- </com.helloandroid.canvastutorial.Panel>
- [...]
This is your main.xml.
Your main class extends Activity and similar as the following code snip:
- public class Canvastutorial extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
Your second class called Panel.java. This class extends SurfaceView and implements SurfaceHolder.Callback. The interface requires three additional methods we need to implement: surfaceCreated(), surfaceDestroyed(), surfaceChanged().
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- }
Add this constructor to code to inicialize the panel:
- super(context, attrs);
- // TODO Auto-generated constructor stub
- getHolder().addCallback(this);
- canvasthread = new CanvasThread(getHolder(), this);
- setFocusable(true);
- }
And modify the implemented methods to set your threads:
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- canvasthread.setRunning(true);
- canvasthread.start();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- boolean retry = true;
- canvasthread.setRunning(false);
- while (retry) {
- try {
- canvasthread.join();
- retry = false;
- // we will try it again and again...
- }
- }
- }
Add this class to your project to create threads, which will control the drawings. We need a constructor with the Panel and the SurfaceHolder as parameters, a setter to set the variable which keeps the thread running and we need to override the method run().:
- package com.helloandroid.canvastutoria
l; - import android.graphics.Canvas;
- import android.view.SurfaceHolder;
- private SurfaceHolder _surfaceHolder;
- private boolean _run = false;
- _surfaceHolder = surfaceHolder;
- _panel = panel;
- }
- public void setRunning(boolean run) {
- _run = run;
- }
- @Override
- public void run() {
- Canvas c;
- while (_run) {
- c = null;
- try {
- c = _surfaceHolder.lockCanvas(null);
- synchronized (_surfaceHolder) {
- _panel.onDraw(c);
- }
- } finally {
- // do this in a finally so that if an exception is thrown
- // during the above, we don't leave the Surface in an
- // inconsistent state
- if (c != null) {
- _surfaceHolder.unlockCanvasAndPost(c);
- }
- }
- }
- }
- }
The last thing, add an onDraw method to your panel:
If you need my image, you can download from here:
Copy this image to drawable folder and refresh your project.
It will draw a kangoo. :) Next time I will show you some drawing commands. Feel free to download the source code from Here.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@fraufletscher (Jessica Griebsch)I've just received an achievement: Affable http://t.co/ZobME8Cv #Android #Androidgames
9 min 44 sec ago -
@mark_pawlowski (mark pawlowski)#IPhone apps crash more than #Android apps. Who would have known?
http://t.co/8ln0iopQ9 min 51 sec ago -
@Labelmars (Label MARS PROJECT)MASAKI YODA on #Spotify Title--Voltage- http://t.co/zKs2TktH #music #musik #iPhone #Android 20-42-14
9 min 52 sec ago -
@180rad (paul radcliffe)I've just received an achievement: Lover of space https://t.co/f4Mcp9dW #Android #Androidgames
9 min 52 sec ago -
@Labelmars (Label MARS PROJECT)MASAKI YODA on #Spotify Title-FREEDOM- http://t.co/hbx947eL http://t.co/Nz84sxao #music #musik #iPhone #Android 20-41-54
10 min 11 sec ago
Poll
Useful resources
Android Development Projects
- Android App wanted immediately by JoePublic
- LIST DATA PROJ by nhammoud
- Nonpublic project #1433932 by subpariq
- Alarm Android Application Design by globalheed
- Simple Album App for Android by ayfonfan
- iOS and Android photo manipulation 'Morph App' by whatwedomedia
- Onsite Software Engineers in Germany by sudhirshree
- Augmented reality by merder99
- Nonpublic project #1433560 by vobla73
- Mobile app coder needed for quick, simple app by Ergometrix




Comments
دردشة سورية دردشة
دردشة سورية
دردشة لبنانية
دردشة عراقية
شات سوري
شات لبناني
دردشة سوريا
دردشة لبنان
شات سوريا
شات لبنان
دردشة السويدي
منتديات السويدي
اغاني عراقية
صور فنانين
الرياضة العراقية
شعراء العراق
نغمات عراقية
اغاني عربية
اغاني كردية
دردشة عراقية
دردشة بنات العراق
دردشة صبايا بغداد
دردشة البصره
دردشة بغداد
دردشة بغدادية
دردشة صبايا بغداد
دردشة شباب العراق
دردشة بنات العراق
دردشة الكرادة
دردشة دمشق
دردشة بيروت
دردشة حلب
دردشة حلب
دردشة عراقية
دردشة العراق
شات عراقي
جات عراقي
دردشه عراقيه
دردشة صبايا لبنان
دردشة بنات لبنان
شات صوتي | دردشة صوتية | كلام
شات صوتي
| دردشة صوتية
|
كلام
| شات كلام
|
دردشة كلام
| دردشة صوتية
|
شات صوتي
| شات
|
Chat Voice
| ahj w,jd
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
دردشه
| دردشة
|
صوتي
| صوتية
|
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
الكلام
| دردشه صوتيه
|
]v]am w,jdm
| ]v]ai w,jdi
شات صوتي
شات صوتي
شات صوتي
thanks
thanks for your article its really great and useful
sportbike specifications SKEMA RANGKAIAN Electronic Circuit scolarship wireless car specifications gadget and computer car first look Motorcycle Modification sexi-stars. newlaptop circuits-audio electronic projects auto girl weapons system science kids gadget bikes walpaper mobil-wallpaper MODIFIKASI MOBIL plane-wallpaper celebsexypict artist-scandal sport-cars bikespict motor-modify sexi-model laptopharga gambar-artist fast-superbike autoshowmodel harga-kamera notebook-price diamonds-collections phones-gallery camera-prices home-pic audio-gallery autocarpict living-cares homes life-style
If I were to add the above
If I were to add the above code to a program that has multiple layouts 1 main layout and horizontal and a vertical layout. How does the above code determine which layout to use? Not sure if this is able to work that way?
micheal
nono
شات صوتي
دردشة صوتية
دردشة
دردشه
شات سعودي
شات خليجي
سكر بنات
جات
شات صوتي سعودي خليجي
chat voice
ahj
خليجي الصوتي
سعودي الصوتي
دردشة صوتي
شات صوتي
دردشة صوتية
شات كتابي
شات كتابي خليجي
شات عسل الصوتي
دردشة كتابية
chat
سعودي كول
سعودي كول 6666
كول
سعودي
سعودي كول انحراف
سعودي كول بنات
سعودي كول 1994
سعودي كول 94
شات سعودي كول
سعودي انحراف
سعودي انحراف2010
سعودي انحراف الصوتي
شات سعودي انحراف
دردشة سعودي انحراف
سعودي انحراف الصوتية
شبكة سعودي انحراف
سعودي انحراف الاصلي
سعودي انحراف كول
سعودي انحراف 2010
انحراف سعودي
saudideviation
دردشة صوتية سعوديه
دردشة صوتية سعودية
دردشة كتابية
دردشة كتابية خليجية
شات
دردشة
خاص للبنات
عرب ذوق
عرب ذوق الصوتي
عرب ذوق الصوتية
دردشة عرب ذوق
شات عرب ذوق
شبكة عرب ذوق
شات صوتي بنات
شات بنات الصوتي
دردشة بنات الصوتي
Girls Chat
شبكة عفناك
صوتية عفناك
شات عفناك
دردشة عفناك
عفناك الصوتي
دردشة عفناك
الخيال
الخيال كام
شبكة الخيال
الخيال الصوتي
الخيال الصوتية
دردشة الخيال
الخيال الصوتية
دردشة صوتية الخيال
شات سعودي خليجي
منتدى نونو
منتدى
منتديات
موقع
شبكة
نونو
Chat Nono
ahj w,jd
]v]am w,jdm
دليل مواقع ويب
دليل مواقع
دليل
مواقع
بنت كول
بنت كول الصوتي
شات بنت كول
دردشة بنت كول
شات بنت كول الصوتية
بنت كول الصوتيه
سعودي كول
صوتية سعودي كول
شات سعودي كول
دردشة سعودي كول
سعودي كول الصوتي
سعودي كول 6666
سعودي كول6666
سكر بنات
شات صوتي زين
شات صوتي ملوك
شات صوتي سعودي
شاتات صوتيه
مكتبة ماسنجر
شات صوتي حبي
شات صوتي كويت
YouTube - Broadcast Yourself. , اليوتيوب نونو
صيف كام
شات صوتي كول
شات انحراف
وه بس
خريطة الموقع نونو
الرياض كول الصوتي
كامات 6666
شات المها
كامات6666
شات كامات 6666
كامات 666
كامات 66
سعودي انحراف
شاتكامات6666
سعودي احوه
شات سعودي احوه
سعودي احوه الصوتي
سعودي احوه كول
دردشة سعودي احوه
احوه سعودي
بنات احوه
دبي الصوتي
سعودي في اي بي الصوتي
شبكة الرياض الصوتي
روعة الليل
لايف كام
الخليج كام
شات كان زمان الصوتي
شات صوتي قصيمي
شات قلبي
ارجوان
شات صوتي قطري
بدور الخليج
منتدى روح
شبكة روح
روح ديزاين
تحميل ماسنجر بلس
توبيكات حزينه
توبيكات
ماسنجر
ماسنجر بلس
تحميل ماسنجر
توبيكات رومنسيه
منتديات روح
دردشة
شات سعودي
خليجي
شات صوتي
سعودي انحراف 2010 , صوتية سعودي انحراف , شبكة سعودي انحراف , موقع سعودي انحراف , سعودي انحراف لايف , مواقع انحرافية , دردشة شات سعودي انحراف الصوتي , سعودي لايف 2010 , سعودي انحراف 2011 , بنات لايف , منحرفات لايف جاتسعودي انحراف 6666- سعودي انحراف 2010 - سعودي انحراف2010 - سعودي انحراف 2007 - سعودي فور انحراف - سعودي انحراف - Saudi an7raf سعودي انحراف , deviation Saudi 2010 دردشة, شات, سعودي, انحراف 2010, كامات, ,شات انحرافي saudi, an7raf, deviation. سعودي انحراف , انحراف , An7raf 6666سعودي انحراف , شات سعودي انحراف , شات انحرافي , انحراف 2010سعودي انحراف , سعودي انحراف 2010 , chat saudi an7raf , saudi ...
Merci à l'information reçue,
!
In this way i can use canvas
In this way i can use canvas on my android smartphone, thanks a lot!
Mercedes
You can make use of many
You can make use of many software programs to do sewing and canvasing but there is no substitute for hand-made art work. If you are keen on learning quilt patterns then you can learn basics on TV while opting for classes later on.
Thank alot for this code and
Thank alot for this code and information.
GSM Kampanya | Bilişim Teknoloji | Cep Telefonu | Turkcell Kampanyaları | Avea Kampanyaları | Vodafone Kampanyaları
Completely I share your opini
Completely I share your opini on. In it something is and it is excellent idea. I support you.
itunes drm remover, itunes video converter, convert dvd to itunes how to put youtube video on ipod touch.
تسريحات منتديات مكياج ازياء
تسريحات منتديات مكياج ازياء رسائل وسائط مسجات توبيكات وظيفة وظائف ديكور بلايز رجيم رشاقة حلويات معجنات نكت ديكورات لاين سبورت رمزيات صور رمزيه توبيكات ملونه صور العاب
Well, I am so excited that I
Well, I am so excited that I have found this your post because I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject. the diet solution
Awesome blog. I enjoyed
Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
garden treasures
Excellent read, I just passed
Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: kitchen worktops
We can give them a good
We can give them a good education that they can use in their life to get a better chance in this life. Im sure with a good education all of us will see the changing that all that kids can have. Mac DVD Ripper
Companies these days are
Companies these days are using ergonomic furniture to ensure that there are no injuries from work related hazards. Use an ergonomic chair and feel better comfort in your back and shoulder. oxyhives
If I add f.e. ImageView to
If I add f.e. ImageView to main.xml I can simply get to it from Canvastutorial class by mageView);
ImageView iv = (ImageView)findViewById(R.id.i
How can I do this from Panel class?
We can give them a good
We can give them a good education that they can use in their life to get a better chance in this life. Im sure with a good education all of us will see the changing that all that kids can have. Ross Mccarthy
There is an article that
There is an article that gives great insight to foundational elements when it comes to being able to successfully make money on the internet. wall stickers
if I test this program on a screen that has multiple layouts whe
If I were to add the above code to a program that has multiple layouts 1 main layout and horizontal and a vertical layout. How does the above code determine which layout to use? Not sure if this is able to work that way?
Companies these days are
Companies these days are using ergonomic furniture to ensure that there are no injuries from work related hazards. Use an ergonomic chair and feel better comfort in your back and shoulder. lexikon
Companies these days are
Companies these days are using ergonomic furniture to ensure that there are no injuries from work related hazards. Use an ergonomic chair and feel better comfort in your back and shoulder. Mattress Protector
it is sometimes hard to
it is sometimes hard to Pandora in a world of adults Pandora Bracelets
Thank alot for this code and
Thank alot for this code and information.
Fitness
ask about declaration
I'm new for android.
when I read your code I don't understand this declaration in xml file
com.helloandroid.canvastutoria l.Panel 1" android:layout_width="wrap_con tent" ontent" al.Panel
android:id="@+id/SurfaceView0
android:layout_height="wrap_c
android:maxHeight="40dip">
com.helloandroid.canvastutori
what does "com.helloandroid.canvastutori al.Panel " mean, I've never seen the declaration like this before?
Can you explain how the program run?
Thanks you very much
RE: ask about declaration
hey asbk1987
com.helloandroid.canvastutoria l is your project name and the Panel is the class you want to show in that project.
greets
MatthiasW