MffiM g Bft View

WTOMM Button * ListView View ftA^ ' MM Android ffi^M

Android Canvas Drawline Activity

Step-1: ^A Android MfMA*^ : ex10 = Step-2: MM Activity iftA^ ex10 > ■

// ---- ex10.java g^i package com.misoo.ex10; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.RadioButton;

public class ex10 extends Activity implements OnClickListener { /** Called when the activity is first created. */ private final int WC = ViewGroup.LayoutParams. WRAP_CONTENT; Draw View dv; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

LinearLayout layout = new LinearLayout(this); layout. setOrientation(LinearLay out. HORIZONTAL); LinearLayout.LayoutParams param =

new LinearLayout.LayoutParams(150, 300); param. leftMargin = 1; dv = new DrawView(this); layout. addView(dv, param);

RadioButton ra;

ra = new RadioButton(this);

param = new LinearLayout.LayoutParams(WC, WC); param. topMargin = 40; layout. addView(ra, param);

ra.setOnClickListener(this); setContentView(lay out);

public void onClick(View arg0) { dv.invalidate(); }

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View;

public class DrawView extends View { private Paint pa = new Paint(); private boolean yn = false;

public DrawView(Context context) { super(context);

@Override protected void onDraw(Canvas canvas) {

if(yn) pa. setColor( Color. GRAY); else pa. setColor(Color. WHITE); canvas.drawRect(10, 10, 100, 100, pa); pa. setColor(Color. YELLOW);

if(yn) pa. setColor( Color. YELLOW); else pa. setColor(Color.5LUE); pa. setStroke Width(4); pa. setStrokeCap(Paint. Cap.ROUND);

canvas.drawLine(10, 55, 100, 55, pa); canvas.drawLine(55, 10, 55, 100, pa); pa. setColor(Color.RED); canvas.drawRect(50, 50, 60, 60, pa);

WMmmSMM®^® Context 'Xmmmm Activity I^iiastftfe" mtAndroid g^mSAlp«®:

Button btnl = new Button(this); Button btn2 = new Button(this);

^ffi Context(^iM Activity)ft#^(SP this){fMläMMXft Button ^ = Context M View '

Button * ListView * RadioButton > aiiWMMMX ° mmXffimft m^:

RadioButton ra;

ra = new RadioButton(this);

param = new LinearLayout.LayoutParams(WC, WC);

param.topMargin = 40; layout. addView(ra, param);

M$nAffiMffi ' mg^MMT DrawView i^m ' AftAA® Button * TextView = {Ma«AaASmffiJnAAAAfifta

A'ASftAffiMffiftfflaA °

10. MMDrawViewi^mAM ' ali^ia^a ' AAJPAiMffi ' M$PAM A:

LinearLayout.LayoutParams param =

new LinearLayout.LayoutParams(150, 300); param.leftMargin = 1; dv = new DrawView(this); layout. addView(dv, param);

<<Google Android

2. Android C IAAPpWMA

3. M^AAAflftlfifAttAAndroid^SA

RadioButton ftMfè ' AMAM^-ft RadioButton MB °

n* ' RadioButton mn^MimtmM • unrn fttt^ ° mm •

HA-tÄIfttt^i ' I^ölSi RadioButton ff ^-ffi ' RadioGroup = MMi RadioButton IS^ftZl- > ' Rfä-

2. m^®T<Change Color>gffi ' w^u^ RadioButton > A^^-i RadioGroup ' $PT :

3. ïiin ' ' i

package com.misoo.ex11;

import android.app.Activity;

import android.graphics.Color;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

public class ex11 extends Activity implements OnCheckedChangeListener { private final int WC = RadioGroup.LayoutParams. WRAP_CONTENT; private RadioGroup rg_layout; private DrawView dv; private LinearLayout layout; private int mColor = Color. YELLOW; private RadioGroup.LayoutParams params; private LinearLayout.LayoutParams para;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); this.show_layout_01(); } public void show_layout_01(){

layout = new LinearLayout(this); layout.setOrientation(LinearLayout. VERTICAL); para = new LinearLayout.LayoutParams(230, 140); dv = new DrawView(this); layout.addView(dv, para); Button btn = new Button(this); Drawable dw; if(mColor == Color.BLUE)

dw = this.getResources0.getDrawable(R.drawable.cAange_fo_ye//ow); else dw = this.getResources()getDrawable(R.drawable.cAange_fo_WMe); btn.setText("Change Color");

btn.setBackgroundDrawable(dw);

btn. setOnClickListener(listener);

para = new LinearLayout.LayoutParams(WC, WC);

layout.addView(btn, para);

rg_layout = new RadioGroup(this); params = new RadioGroup.LayoutParams(WC, WC); rg_layout.setOrientation(RadioGroup. VERTICAL); rg_lay out. setLay outParams (params); rg_lay out. setOnCheckedChangeListener(this); RadioButton buttonl = new RadioButton(this); button1.setText("Yellow"); buttonl.setId(1001); params = new RadioGroup.LayoutParams(WC, WC); rg_lay out. addView (buttonl, params); RadioButton button2 = new RadioButton(this); button2.setText("Blue"); button2.setId(1002); params = new RadioGroup.LayoutParams(WC, WC); rg_lay out. addView (button2, params); setContentView(lay out);

public int getColor() { return mColor; }

private OnClickListener listener = new OnClickListener() {

public void onClick(View v) { setContentView(rg_layout); }

public void onCheckedChanged(RadioGroup arg0, int arg1) {

if(arg0.getCheckedRadioButtonId() == 1001) mColor = Color.YELLOW; else mColor = Color.BLUE; this. show_lay out_01 ();

Step-3: MM View ftX^^ : DrawView ' Sg^I^T : // — DrawView.java g^I

package com.misoo.ex11; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View;

public class DrawView extends View { private Paint pa; private Context ctx;

public DrawView(Context context) { super(context);

ctx = context; pa = new Paint(); } @Override protected void onDraw(Canvas canvas) { ex11 obj = (ex11)ctx;

pa.setColor(obj.getColor()); canvas.drawRect(10, 10, 100, 100, pa); pa.setColor(Color. GREEN); pa.setStrokeWidth(4); pa.setStrokeCap(Paint.Cap.ROUND);

canvas.drawLine(10, 55, 100, 55, pa); canvas.drawLine(55, 10, 55, 100, pa);

1. RadioGroup LinearLayout > fBgAHftAM—iSft View ' ff g RadioButton ' View =

3. m^M^m layout > #®T<Change Color>^» ' MfMAW onClick()®^ : public void onClick(View v) {

setContentView(rg_layout);

^T^bA : setContentView(rg_layout); rg_layout °

4. rg_layout «ffi^MAWMffl RadioButton > ^M^- . W&ZM ' g^ffMA

public void onCheckedChanged(RadioGroup arg0, int arg1) { if(arg0.getCheckedRadioButtonId() == 1001)

mColor = Color. YELLOW; else mColor = Color. BLUE; setContentView(layout); }

ft^ff^SRadioButtonftlDA^K^MA^ESTM^W-fflRadioButton =

Was this article helpful?

0 0

Post a comment