Sunday, 29 December 2013

TUTORIAL PEMBUATAN CANVAS PADA ADT BUNDLE ANDROID



  1. Buat Projek Baru.
  2. Isikan Kriteria dan desain sesuai yang anda inginkan.
  3. Ketikan Coding dibawah ini pada layout mainActivity.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


</RelativeLayout>


  1. Pilih gambar dan masukan pada folder xxhdpi.
  2. Isikan coding berikut pada Activity Main pada src.
package com.example.canvas2;


import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new canvas7(this));
}

static public class canvas7 extends View{
Bitmap gambarkudangambarmu;

public canvas7(Context context){
super(context);
gambarkudangambarmu = BitmapFactory.decodeResource(context.getResources(),R.drawable.aaa);
}
@SuppressLint("DrawAllocation")
protected void onDraw(Canvas canvas){
Paint paint=new Paint();
canvas.drawBitmap(gambarkudangambarmu, 100, 200,paint);
}
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



  1. Pada coding R.drawable.aaa,”aaa” diganti dengan nama gambar yang anda masukan tadi pada folder xx-hdpi.
  2. Output :

0 komentar:

Post a Comment

Note: only a member of this blog may post a comment.