custom view

custom view
master
hmkcode 11 years ago
parent 9f709aed2d
commit 6c8d212481

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hmkcode.views"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

@ -0,0 +1,38 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:customviews="http://schemas.android.com/apk/res/com.hmkcode.views"
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"
android:orientation="vertical"
tools:context="com.hmkcode.views.MainActivity"
>
<com.hmkcode.views.SimpleView
android:id="@+id/simpleViewCircle"
android:layout_width="140dp"
android:layout_height="150dp"
customviews:shape="circle"
customviews:dim="70dp"
android:layout_gravity="center_horizontal"
/>
<com.hmkcode.views.SimpleView
android:id="@+id/simpleViewSquare"
android:layout_width="140dp"
android:layout_height="140dp"
customviews:shape="square"
customviews:dim="140dp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>

@ -0,0 +1,9 @@
<resources>
<declare-styleable name="SimpleView">
<attr name="shape" format="enum">
<enum name="circle" value="0"/>
<enum name="square" value="1"/>
</attr>
<attr name="dim" format="dimension"/>
</declare-styleable>
</resources>

@ -0,0 +1,7 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Simple Custom View</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>

@ -0,0 +1,20 @@
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>

@ -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;
}
}
}

@ -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;
}
}
}
Loading…
Cancel
Save