CS 696 Mobile Phone Application DevelopmentFall Semester, 2009Doc 15 Network DataOct 20, 2009Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.2D Graphic Options2Draw graphics/animation on ViewsSimple graphicsSimple animationsDraw on a canvasGraphics need to be redrawnGames3Draw graphics/animation on ViewsDisplay a Drawable4public class GraphicsExamples extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linear = new LinearLayout(this); ImageView image = new ImageView(this); image.setImageResource(R.drawable.dog); image.setAdjustViewBounds(true); image.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); linear.addView(image); setContentView(linear); }}Display a Drawable - using XML5public class GraphicsExamples extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}main.xml6<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="The Dog" android:layout_gravity="center_horizontal"/><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/dog" android:layout_gravity="center_horizontal"/></LinearLayout>Transitions7Activity8public class GraphicsExamples extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources resources = this.getResources(); TransitionDrawable transition = (TransitionDrawable) resources .getDrawable(R.layout.transition); ImageView image = (ImageView) findViewById(R.id.image); image.setImageDrawable(transition); transition.startTransition(5000); }}layout/transition.xml9<transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/dog"/> <item android:drawable="@drawable/fog"/></transition>Background of a View10public class GraphicsExamples extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources resources = this.getResources(); Drawable dog = resources.getDrawable(R.drawable.dog); View image = findViewById(R.id.simpleView); image.setBackgroundDrawable(dog); }}Drawing an Oval11public class GraphicsExamples extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View shapes = new SimpleDrawing(this); setContentView(shapes); }}SimpleDrawing12public class SimpleDrawing extends View { private ShapeDrawable oval; public SimpleDrawing(Context context) { super(context); int x = 10; int y = 10; int width = 300; int height = 50; this.oval = new ShapeDrawable(new OvalShape()); this.oval.getPaint().setColor(0xff74AC23); this.oval.setBounds(x, y, x + width, y + height); }SimpleDrawing13 public SimpleDrawing(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { this.oval.draw(canvas); }}Colors14(alpha << 24) | (red << 16) | (green << 8) | blue0xff74AC23Color value range 0..255blue = 0x23 (35)green = 0xAC (172)red = 0x74 (116)alpha = 0xff (255)Coordinates15(0,0)xy(30,100)Shapes16PathShapeRectShapeArcShapeOvalShapeRoundRectShapeTween Animation17<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromYDelta="0" android:toXDelta="800%" android:duration="1000" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" /></set>res/anim/tween_example.xmlActivity18public class GraphicsExamples extends Activity implements View.OnClickListener { public void onClick(View v) { Animation shake = AnimationUtils.loadAnimation(this, R.anim.tween_example); findViewById(R.id.go).startAnimation(shake); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View go = findViewById(R.id.go); go.setOnClickListener(this); }}main.xml19<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="Go" android:id="@+id/go" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>android.view.animation.Interpolator20AccelerateDecelerateInterpolatorAccelerateInterpolatorAnticipateInterpolatorAnticipateOvershootInterpolatorBounceInterpolatorCycleInterpolatorDecelerateInterpolatorLinearInterpolatorOvershootInterpolatorDefines the rate of change of an animationTween XML Attributes21<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromYDelta="0" android:toXDelta="800%" android:duration="1000" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" /></set>android.view.animation.Animation22android:durationandroid:fillAfterandroid:fillBeforeandroid:fillEnabledandroid:interpolatorandroid:repeatCountsetRepeatCount(int)setRepeatMode(int)android:startOffsetandroid:zAdjustmentDefined in Animation ClassConstructors as XML23AlphaAnimation(float fromAlpha, float toAlpha)<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />Frame Animation24res/anim/frame_animation.xml25<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/dog" android:duration="900" /> <item
View Full Document