custom view
custom view
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user