From 6c8d212481a4425f4bbefe51ee97dd7b2737dcf1 Mon Sep 17 00:00:00 2001 From: hmkcode Date: Fri, 30 Jan 2015 18:38:49 +0300 Subject: [PATCH] custom view custom view --- android-custom-view/AndroidManifest.xml | 27 +++++++++ .../res/layout/activity_main.xml | 38 +++++++++++++ android-custom-view/res/values/attrs.xml | 9 +++ android-custom-view/res/values/dimens.xml | 7 +++ android-custom-view/res/values/strings.xml | 8 +++ android-custom-view/res/values/styles.xml | 20 +++++++ .../src/com/hmkcode/views/MainActivity.java | 48 ++++++++++++++++ .../src/com/hmkcode/views/SimpleView.java | 57 +++++++++++++++++++ 8 files changed, 214 insertions(+) create mode 100644 android-custom-view/AndroidManifest.xml create mode 100644 android-custom-view/res/layout/activity_main.xml create mode 100644 android-custom-view/res/values/attrs.xml create mode 100644 android-custom-view/res/values/dimens.xml create mode 100644 android-custom-view/res/values/strings.xml create mode 100644 android-custom-view/res/values/styles.xml create mode 100644 android-custom-view/src/com/hmkcode/views/MainActivity.java create mode 100644 android-custom-view/src/com/hmkcode/views/SimpleView.java diff --git a/android-custom-view/AndroidManifest.xml b/android-custom-view/AndroidManifest.xml new file mode 100644 index 0000000..d363509 --- /dev/null +++ b/android-custom-view/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + diff --git a/android-custom-view/res/layout/activity_main.xml b/android-custom-view/res/layout/activity_main.xml new file mode 100644 index 0000000..3344066 --- /dev/null +++ b/android-custom-view/res/layout/activity_main.xml @@ -0,0 +1,38 @@ + + + + + + + + diff --git a/android-custom-view/res/values/attrs.xml b/android-custom-view/res/values/attrs.xml new file mode 100644 index 0000000..a97caa4 --- /dev/null +++ b/android-custom-view/res/values/attrs.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/android-custom-view/res/values/dimens.xml b/android-custom-view/res/values/dimens.xml new file mode 100644 index 0000000..55c1e59 --- /dev/null +++ b/android-custom-view/res/values/dimens.xml @@ -0,0 +1,7 @@ + + + + 16dp + 16dp + + diff --git a/android-custom-view/res/values/strings.xml b/android-custom-view/res/values/strings.xml new file mode 100644 index 0000000..eed0eaf --- /dev/null +++ b/android-custom-view/res/values/strings.xml @@ -0,0 +1,8 @@ + + + + Simple Custom View + Hello world! + Settings + + diff --git a/android-custom-view/res/values/styles.xml b/android-custom-view/res/values/styles.xml new file mode 100644 index 0000000..845fb57 --- /dev/null +++ b/android-custom-view/res/values/styles.xml @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/android-custom-view/src/com/hmkcode/views/MainActivity.java b/android-custom-view/src/com/hmkcode/views/MainActivity.java new file mode 100644 index 0000000..f0b152f --- /dev/null +++ b/android-custom-view/src/com/hmkcode/views/MainActivity.java @@ -0,0 +1,48 @@ +package com.hmkcode.views; + +import android.app.Activity; +import android.os.Bundle; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.TextView; + + +public class MainActivity extends Activity implements OnClickListener { + + SimpleView svCircle; + SimpleView svSquare; + TextView tv; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + svCircle = (SimpleView) findViewById(R.id.simpleViewCircle); + svSquare = (SimpleView) findViewById(R.id.simpleViewSquare); + tv = (TextView) findViewById(R.id.tv); + + svCircle.setOnClickListener(this); + svSquare.setOnClickListener(this); + + } + + + @Override + public void onClick(View view) { + + switch(view.getId()){ + case R.id.simpleViewCircle: + tv.setText("Circle"); + break; + case R.id.simpleViewSquare: + tv.setText("Square"); + break; + + } + + } + + + +} diff --git a/android-custom-view/src/com/hmkcode/views/SimpleView.java b/android-custom-view/src/com/hmkcode/views/SimpleView.java new file mode 100644 index 0000000..121f690 --- /dev/null +++ b/android-custom-view/src/com/hmkcode/views/SimpleView.java @@ -0,0 +1,57 @@ +package com.hmkcode.views; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.CornerPathEffect; +import android.graphics.Paint; +import android.util.AttributeSet; +import android.view.View; + +public class SimpleView extends View { + + float dim; + int shape; + Paint paint; + + public static final int CIRCLE = 0; + public static final int SQUARE = 1; + public SimpleView(Context context, AttributeSet attrs) { + super(context, attrs); + + TypedArray a = context.getTheme().obtainStyledAttributes( + attrs, + R.styleable.SimpleView, + 0, 0 + ); + + try { + dim = a.getDimension(R.styleable.SimpleView_dim, 20f); + shape = a.getInteger(R.styleable.SimpleView_shape, 0); + } finally { + a.recycle(); + } + + paint = new Paint(); + paint.setColor(0xfffed325); // yellow + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + // draw circle + switch(shape){ + case CIRCLE: + canvas.drawCircle(dim, dim, dim, paint); + break; + case SQUARE: + canvas.drawRect(0, 0, dim, dim, paint); + break; + + } + + } + + +}