- 浏览: 150040 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (82)
- android 开发者之路 (11)
- linux yum命令详解 (1)
- Tomcat7 启动报错 …… not found on the java.library.path (1)
- tomcat7.0.22高级应用之apr的安装 (1)
- tomcat (2)
- tomcat打开工程后怎么出现严重 (0)
- tomcat 启动 、严重 Error in resourceStart() 解决办法 (1)
- JAVA 对象池 (1)
- java时间运算 (1)
- ant深入浅出 (1)
- Ant build.xml 详解 (1)
- memcached命令解析 (1)
- 关于 href 中的 target (1)
- java 获取请求 URL (2)
- Tomcat 开发模式配置 (1)
- MemcacheDB启动和清除日志的正确方法 (0)
- MemcacheDB启动和清除日志的方法 (1)
- JuRest (2)
- Shiro (1)
- JUnit (3)
- Webservices (1)
- js (4)
- java 正则 (2)
- java 异常 (1)
- eclipse (1)
- sdfsadfa (0)
- Androidpn (1)
最新评论
-
errorlove:
移除的话如果别的地方要用到原来的证书咋办!我遇到问题了,请问有 ...
keytool错误:java.io.IOException:keystore was tampered with,or password was incorre -
fengweiyou:
写的很好
oracle统计money存储过程
android 自定义ButtonTab ,
ActivityGroup 动态加载 activity
最近几天一直在做公司客户看着默认的 tab 切换着实不爽,查了下资料费了番功夫写了个自定义的 ButtonTab 在此和 广大 android 爱好者分享源码如下。AndroidManifest.xml 写道
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qiamian.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ButtonTabActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestA"/>
<activity android:name=".TestB"/>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qiamian.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ButtonTabActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestA"/>
<activity android:name=".TestB"/>
</application>
</manifest>
ButtonTabActivity 写道
package com.qiamian.test;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import com.qiamian.test.SegmentControl.OnSegmentChangedListener;
/**
* @author 轻描淡写
*/
public class ButtonTabActivity extends ActivityGroup{
SegmentControl segControl;
private ViewFlipper mFlipper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mFlipper = (ViewFlipper)findViewById(R.id.flipper);
segControl = (SegmentControl)findViewById(R.id.segcontrol);
segControl.setStyle(SegmentControl.TAB);//试试SEGMENT
segControl.newButton("标题1", 0);
segControl.newButton("标题2", 1);
segControl.newButton("标题3", 2);
segControl.newButton("标题4", 3);
//还可试试segControl.newButton(int drawableId, int id);
segControl.setSelectedIndex(0);
int width = this.px2dip(this, 80*segControl.getButtonCount());
int height = this.px2dip(this, 38);
segControl.setWidth(width, height, segControl.getButtonCount());
segControl.setOnSegmentChangedListener(new OnSegmentChangedListener() {
@Override
public void onSegmentChanged(int index) {
//
onChangeView(index);
}
});
SwitchActivity(0);//默认打开第0页
}
private void onChangeView(int index)
{
//测试界面,实际开发中是从layout中读取的,下同。
TextView tv=new TextView(this);
tv.setText("index="+index);
switch(index){
case 0:
Toast.makeText(this, "VIEW_TLINE", Toast.LENGTH_SHORT).show();
SwitchActivity(1);
break;
case 1:
Toast.makeText(this, "VIEW_KLINE", Toast.LENGTH_SHORT).show();
SwitchActivity(0);
break;
case 2:
Toast.makeText(this, "VIEW_DETAIL", Toast.LENGTH_SHORT).show();
SwitchActivity(1);
break;
case 3:
Toast.makeText(this, "VIEW_F10", Toast.LENGTH_SHORT).show();
SwitchActivity(0);
break;
case 4:
Toast.makeText(this, "VIEW_RADAR", Toast.LENGTH_SHORT).show();
SwitchActivity(1);
break;
}
}
/**
* 根据ID打开指定的Activity
* @param id GridView选中项的序号
*/
void SwitchActivity(int id)
{
mFlipper.removeAllViews();//必须先清除容器中所有的View
Intent intent =null;
if (id == 0 || id == 2) {
intent = new Intent(ButtonTabActivity.this, TestA.class);
} else if (id == 1 || id == 3) {
intent = new Intent(ButtonTabActivity.this, TestB.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Activity 转为 View
Window subActivity = getLocalActivityManager().startActivity(
"subActivity", intent);
//容器添加View
mFlipper.addView(subActivity.getDecorView(),
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
//dip/px像素单位转换
public int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue / scale + 0.5f);
}
public int px2dip(Context context, float pxValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(pxValue * scale + 0.5f);
}
}
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import com.qiamian.test.SegmentControl.OnSegmentChangedListener;
/**
* @author 轻描淡写
*/
public class ButtonTabActivity extends ActivityGroup{
SegmentControl segControl;
private ViewFlipper mFlipper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mFlipper = (ViewFlipper)findViewById(R.id.flipper);
segControl = (SegmentControl)findViewById(R.id.segcontrol);
segControl.setStyle(SegmentControl.TAB);//试试SEGMENT
segControl.newButton("标题1", 0);
segControl.newButton("标题2", 1);
segControl.newButton("标题3", 2);
segControl.newButton("标题4", 3);
//还可试试segControl.newButton(int drawableId, int id);
segControl.setSelectedIndex(0);
int width = this.px2dip(this, 80*segControl.getButtonCount());
int height = this.px2dip(this, 38);
segControl.setWidth(width, height, segControl.getButtonCount());
segControl.setOnSegmentChangedListener(new OnSegmentChangedListener() {
@Override
public void onSegmentChanged(int index) {
//
onChangeView(index);
}
});
SwitchActivity(0);//默认打开第0页
}
private void onChangeView(int index)
{
//测试界面,实际开发中是从layout中读取的,下同。
TextView tv=new TextView(this);
tv.setText("index="+index);
switch(index){
case 0:
Toast.makeText(this, "VIEW_TLINE", Toast.LENGTH_SHORT).show();
SwitchActivity(1);
break;
case 1:
Toast.makeText(this, "VIEW_KLINE", Toast.LENGTH_SHORT).show();
SwitchActivity(0);
break;
case 2:
Toast.makeText(this, "VIEW_DETAIL", Toast.LENGTH_SHORT).show();
SwitchActivity(1);
break;
case 3:
Toast.makeText(this, "VIEW_F10", Toast.LENGTH_SHORT).show();
SwitchActivity(0);
break;
case 4:
Toast.makeText(this, "VIEW_RADAR", Toast.LENGTH_SHORT).show();
SwitchActivity(1);
break;
}
}
/**
* 根据ID打开指定的Activity
* @param id GridView选中项的序号
*/
void SwitchActivity(int id)
{
mFlipper.removeAllViews();//必须先清除容器中所有的View
Intent intent =null;
if (id == 0 || id == 2) {
intent = new Intent(ButtonTabActivity.this, TestA.class);
} else if (id == 1 || id == 3) {
intent = new Intent(ButtonTabActivity.this, TestB.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Activity 转为 View
Window subActivity = getLocalActivityManager().startActivity(
"subActivity", intent);
//容器添加View
mFlipper.addView(subActivity.getDecorView(),
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
//dip/px像素单位转换
public int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue / scale + 0.5f);
}
public int px2dip(Context context, float pxValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(pxValue * scale + 0.5f);
}
}
package com.qiamian.test;
import android.app.Activity; import android.os.Bundle; /** * @author 轻描淡写 */ public class TestA extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.test_a); } }
package com.qiamian.test;
import android.app.Activity; import android.os.Bundle; /** * 恰面网 * www.qiamian.com * @author 轻描淡写 */ public class TestB extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.test_b); } }
package com.qiamian.test;
import java.util.HashMap; import java.util.Map; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; /** * @author 轻描淡写 */ public class SegmentControl extends LinearLayout { private Map<Integer,DivButton> indexButtonMap = new HashMap<Integer, DivButton>(); private Map<DivButton,Integer> buttonIndexMap = new HashMap<DivButton, Integer>(); private int selectedIndex; public static final int TAB = 1; public static final int SEGMENT = 2; private int currentStyle = SEGMENT; private int maxButtonSize; private int marginsLeft = 1; private LayoutParams layoutMarginsParams; private boolean onlyIsPressed; private OnSegmentChangedListener onSegmentChangedListener; public SegmentControl(Context context, AttributeSet attrs) { super(context,attrs); this.setOrientation(HORIZONTAL); layoutMarginsParams = new LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutMarginsParams.setMargins(marginsLeft, 0, 0, 0); } public SegmentControl(Context context,int style) { super(context,null); this.setOrientation(HORIZONTAL); currentStyle = style; layoutMarginsParams = new LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutMarginsParams.setMargins(marginsLeft, 0, 0, 0); } public void setStyle(int style) { currentStyle = style; } public void setWidth(int width, int height, int num) { int itemWidth = width/num; layoutMarginsParams.width = itemWidth; layoutMarginsParams.height = height; } public int getButtonCount(){ return maxButtonSize; } public DivButton getButton(int index){ return indexButtonMap.get(index); } public void setSelectedIndex(int index){ if(index <= maxButtonSize){ selectedIndex = index; selectButton(index); } } public int getSelectedIndex(){ return selectedIndex; } /** * 废弃 * 请使用setOnSegmentChangedListener代替 * @param index * @param l */ @Deprecated public void bindOnChooseListener(int index, DivButton.OnChooseListener l){ indexButtonMap.get(index).setOnChooseListener(l); } public void clearButton() { this.removeAllViews(); maxButtonSize = 0; } public DivButton newButton(int drawableId, int id){ DivButton button = new DivButton(getContext(), id, DivButton.PICTURE); button.setLayoutParams(layoutMarginsParams); button.setBackgroundResource(drawableId); postNewButton(button); return button; } private void postNewButton(DivButton button){ this.addView(button); addButtonToMap(button, maxButtonSize); maxButtonSize++; button.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { selectedIndex = buttonIndexMap.get(v); selectButton(selectedIndex); } return false; } }); } public DivButton newButton(String text, int id){ DivButton button = null; if(currentStyle == TAB){ button = new DivButton(getContext(), id, DivButton.TAB); }else if(currentStyle == SEGMENT){ if(maxButtonSize == 0){ button = new DivButton(getContext(), id); }else{ button = new DivButton(getContext(), id, DivButton.SEGMENT_CENTER); } //只有2个按钮 if(maxButtonSize == 1){ getButton(0).changeButtonStyle(DivButton.SEGMENT_LEFT); button.changeButtonStyle(DivButton.SEGMENT_RIGHT); } //超过2按钮 if(maxButtonSize > 1){ getButton(0).changeButtonStyle(DivButton.SEGMENT_LEFT); getButton(maxButtonSize - 1).changeButtonStyle(DivButton.SEGMENT_CENTER); button.changeButtonStyle(DivButton.SEGMENT_RIGHT); } } //layoutMarginsParams = new LayoutParams(45, 35); button.setLayoutParams(layoutMarginsParams); //button背景色可以在这里设置 button.setPressedColor(Color.rgb(16, 38, 55), Color.rgb(16, 38, 55)); button.setTextSize(16); button.setText(text); postNewButton(button); return button; } private void addButtonToMap(DivButton button, int index){ this.indexButtonMap.put(maxButtonSize, button); this.buttonIndexMap.put(button, maxButtonSize); } private void selectButton(int index){ //1 if(maxButtonSize == 1){ DivButton button = indexButtonMap.get(0); button.onDefaultUp(); if(!onlyIsPressed){ button.onDown(); if(button.hasPressedDrawable()){ button.setPressedDrawable(); } if(onSegmentChangedListener != null){ onSegmentChangedListener.onSegmentChanged(button.getCmdId()); } onlyIsPressed = true; }else{ if(button.hasDefaultDrawable()){ button.setDefaultDrawable(); } button.onUp(); onlyIsPressed = false; } //more }else{ for (int i = 0; i < maxButtonSize; i++) { DivButton button = indexButtonMap.get(i); if(i == index){ if(button.isNormal()){ button.onDown(); if(button.hasPressedDrawable()){ button.setPressedDrawable(); } if(onSegmentChangedListener != null){ onSegmentChangedListener.onSegmentChanged(button.getCmdId()); } } }else{ if(button.hasDefaultDrawable()){ button.setDefaultDrawable(); } button.onDefaultUp(); } } } } public interface OnSegmentChangedListener{ public void onSegmentChanged(int index); } public void setOnSegmentChangedListener(OnSegmentChangedListener l){ this.onSegmentChangedListener = l; } }
DivButton 写道
package com.qiamian.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
/**
* 恰面网
* www.qiamian.com
* @author 轻描淡写
*/
public class DivButton extends Button implements OnTouchListener {
private int buttonID;
private ShapeDrawable mDrawable;
private boolean isPressed = false;
private int radian;
float[] DEFAULT_OUTRADII;
float[] TAB_OUTRADII;
float[] LEFT_OUTRADII;
float[] RIGHT_OUTRADII;
float[] CENTER_OUTRADII;
private static int DEFAULT_RADIAN = 8;
//#B7B7B7
private int DEFAULT_START_COLOR = -4737097;
//8F8F8F
private int DEFAULT_END_COLOR = -7368817;
//#9F9F9F
private int PRESSED_START_COLOR = -6316129;
//#767676
private int PRESSED_END_COLOR = -9013642;
public static int TAB = 1;
public static int SEGMENT_LEFT = 2;
public static int SEGMENT_CENTER = 3;
public static int SEGMENT_RIGHT = 4;
public static int DEFAULT = 0;
public static int PICTURE = 5;
private int style;
private OnChooseListener mOnChooseListener;
/**
* 默认图片
*/
private int defaultDrawableId;
/**
* 按下图片
*/
private int pressedDrawableId;
public boolean hasDefaultDrawable(){
if(defaultDrawableId != 0){
return true;
}else{
return false;
}
}
public boolean hasPressedDrawable(){
if(pressedDrawableId != 0){
return true;
}else{
return false;
}
}
public void setDefaultDrawableId(int defaultDrawableId){
this.defaultDrawableId = defaultDrawableId;
}
public void setDefaultDrawable(int defaultDrawableId){
setDefaultDrawableId(defaultDrawableId);
setDefaultDrawable();
}
public void setDefaultDrawable(){
setBackgroundResource(defaultDrawableId);
}
public void setPressedDrawable(int pressedDrawableId){
setPressedDrawableId(pressedDrawableId);
setPressedDrawable();
}
public void setPressedDrawable(){
setBackgroundResource(pressedDrawableId);
}
public void setPressedDrawableId(int pressedDrawableId){
this.pressedDrawableId = pressedDrawableId;
}
public void setOnChooseListener(DivButton.OnChooseListener l){
this.mOnChooseListener = l;
}
public boolean isNormal(){
return !isPressed;
}
public boolean isPressed(){
return isPressed;
}
public void setRadian(int radian){
this.radian = radian;
initRadian();
changeButtonStyle(style);
}
private void initRadian(){
DEFAULT_OUTRADII = new float[] { radian, radian, radian, radian, radian, radian, radian, radian };
TAB_OUTRADII = new float[] { radian, radian, radian, radian, 0, 0, 0, 0 };
LEFT_OUTRADII = new float[] { radian, radian, 0, 0, 0, 0, radian, radian };
RIGHT_OUTRADII = new float[] { 0, 0, radian, radian, radian, radian, 0, 0 };
CENTER_OUTRADII = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };
}
/**
*
* @param startColor
* @param endColor
*/
public DivButton setNormalColor(int startColor, int endColor){
this.DEFAULT_START_COLOR = startColor;
this.DEFAULT_END_COLOR = endColor;
invalidate();
return this;
}
/**
*
* @param startColor
* @param endColor
*/
public DivButton setPressedColor(int startColor, int endColor){
this.PRESSED_START_COLOR = startColor;
this.PRESSED_END_COLOR = endColor;
invalidate();
return this;
}
private Shader getNormalColor(int width, int height){
return new LinearGradient(width/2,0,width/2,height,DEFAULT_START_COLOR,DEFAULT_END_COLOR,Shader.TileMode.MIRROR);
}
private Shader getPressedColor(int width, int height){
return new LinearGradient(width/2,0,width/2,height,PRESSED_START_COLOR, PRESSED_END_COLOR,Shader.TileMode.MIRROR);
}
public DivButton(Context context, int id, int style) {
super(context,null);
this.buttonID = id;
init(style);
}
public DivButton(Context context, int id){
super(context,null);
this.buttonID = id;
init(DEFAULT);
}
private void init(int style){
radian = DEFAULT_RADIAN;
initRadian();
if(PICTURE != style){
if(mDrawable == null){
mDrawable = getShapeDrawable(style);
}
this.getBackground().setAlpha(0);
this.setTextColor(Color.WHITE);
}
this.setOnTouchListener(this);
}
public DivButton(Context context, int id, AttributeSet attrs) {
super(context,attrs);
this.buttonID = id;
init(DEFAULT);
}
public DivButton(Context context, int id, ShapeDrawable mDrawable){
super(context);
this.buttonID = id;
this.mDrawable = mDrawable;
}
public int getCmdId() {
return buttonID;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(mDrawable != null){
mDrawable.setBounds(0, 0, this.getWidth(), this.getHeight());
if(!isPressed){
mDrawable.getPaint().setShader(getNormalColor(this.getWidth(), this.getHeight()));
}else{
mDrawable.getPaint().setShader(getPressedColor(this.getWidth(), this.getHeight()));
}
//mDrawable.getPaint().setColor(Color.BLUE);
//mDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
mDrawable.draw(canvas);
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(getTextSize());
paint.setColor(Color.WHITE);
FontMetrics fm = paint.getFontMetrics();
int y = getTop() + (int)(getHeight() - fm.ascent)/2;
canvas.drawText((String)getText(), getWidth()/2, y, paint);
}
public void onDown() {
onDefaultDown();
if(mOnChooseListener != null){
mOnChooseListener.onDown();
}
}
public void onUp() {
onDefaultUp();
if(mOnChooseListener != null){
mOnChooseListener.onUp();
}
}
public void onDefaultUp(){
isPressed = false;
invalidate();
}
public void onDefaultDown(){
isPressed = true;
invalidate();
}
public void changeButtonStyle(int style){
getShapeDrawable(style);
invalidate();
}
private ShapeDrawable getShapeDrawable(int style){
this.style = style;
if(style == TAB){
mDrawable = new ShapeDrawable(new RoundRectShape(TAB_OUTRADII, null,
null));
}else if(style == SEGMENT_LEFT){
mDrawable = new ShapeDrawable(new RoundRectShape(LEFT_OUTRADII, null,
null));
}else if(style == SEGMENT_CENTER){
mDrawable = new ShapeDrawable(new RoundRectShape(CENTER_OUTRADII, null,
null));
}else if(style == SEGMENT_RIGHT){
mDrawable = new ShapeDrawable(new RoundRectShape(RIGHT_OUTRADII, null,
null));
}else{
mDrawable = new ShapeDrawable(new RoundRectShape(DEFAULT_OUTRADII, null,
null));
}
return mDrawable;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!isPressed) {
if(hasPressedDrawable()){
setBackgroundResource(pressedDrawableId);
}
// 更改为按下时的背景图
onDown();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (isPressed) {
if(hasDefaultDrawable()){
setBackgroundResource(defaultDrawableId);
}
// 改为抬起时的图片
onUp();
}
}
// TODO Auto-generated method stub
return false;
}
public interface OnChooseListener{
public void onDown();
public void onUp();
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
/**
* 恰面网
* www.qiamian.com
* @author 轻描淡写
*/
public class DivButton extends Button implements OnTouchListener {
private int buttonID;
private ShapeDrawable mDrawable;
private boolean isPressed = false;
private int radian;
float[] DEFAULT_OUTRADII;
float[] TAB_OUTRADII;
float[] LEFT_OUTRADII;
float[] RIGHT_OUTRADII;
float[] CENTER_OUTRADII;
private static int DEFAULT_RADIAN = 8;
//#B7B7B7
private int DEFAULT_START_COLOR = -4737097;
//8F8F8F
private int DEFAULT_END_COLOR = -7368817;
//#9F9F9F
private int PRESSED_START_COLOR = -6316129;
//#767676
private int PRESSED_END_COLOR = -9013642;
public static int TAB = 1;
public static int SEGMENT_LEFT = 2;
public static int SEGMENT_CENTER = 3;
public static int SEGMENT_RIGHT = 4;
public static int DEFAULT = 0;
public static int PICTURE = 5;
private int style;
private OnChooseListener mOnChooseListener;
/**
* 默认图片
*/
private int defaultDrawableId;
/**
* 按下图片
*/
private int pressedDrawableId;
public boolean hasDefaultDrawable(){
if(defaultDrawableId != 0){
return true;
}else{
return false;
}
}
public boolean hasPressedDrawable(){
if(pressedDrawableId != 0){
return true;
}else{
return false;
}
}
public void setDefaultDrawableId(int defaultDrawableId){
this.defaultDrawableId = defaultDrawableId;
}
public void setDefaultDrawable(int defaultDrawableId){
setDefaultDrawableId(defaultDrawableId);
setDefaultDrawable();
}
public void setDefaultDrawable(){
setBackgroundResource(defaultDrawableId);
}
public void setPressedDrawable(int pressedDrawableId){
setPressedDrawableId(pressedDrawableId);
setPressedDrawable();
}
public void setPressedDrawable(){
setBackgroundResource(pressedDrawableId);
}
public void setPressedDrawableId(int pressedDrawableId){
this.pressedDrawableId = pressedDrawableId;
}
public void setOnChooseListener(DivButton.OnChooseListener l){
this.mOnChooseListener = l;
}
public boolean isNormal(){
return !isPressed;
}
public boolean isPressed(){
return isPressed;
}
public void setRadian(int radian){
this.radian = radian;
initRadian();
changeButtonStyle(style);
}
private void initRadian(){
DEFAULT_OUTRADII = new float[] { radian, radian, radian, radian, radian, radian, radian, radian };
TAB_OUTRADII = new float[] { radian, radian, radian, radian, 0, 0, 0, 0 };
LEFT_OUTRADII = new float[] { radian, radian, 0, 0, 0, 0, radian, radian };
RIGHT_OUTRADII = new float[] { 0, 0, radian, radian, radian, radian, 0, 0 };
CENTER_OUTRADII = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };
}
/**
*
* @param startColor
* @param endColor
*/
public DivButton setNormalColor(int startColor, int endColor){
this.DEFAULT_START_COLOR = startColor;
this.DEFAULT_END_COLOR = endColor;
invalidate();
return this;
}
/**
*
* @param startColor
* @param endColor
*/
public DivButton setPressedColor(int startColor, int endColor){
this.PRESSED_START_COLOR = startColor;
this.PRESSED_END_COLOR = endColor;
invalidate();
return this;
}
private Shader getNormalColor(int width, int height){
return new LinearGradient(width/2,0,width/2,height,DEFAULT_START_COLOR,DEFAULT_END_COLOR,Shader.TileMode.MIRROR);
}
private Shader getPressedColor(int width, int height){
return new LinearGradient(width/2,0,width/2,height,PRESSED_START_COLOR, PRESSED_END_COLOR,Shader.TileMode.MIRROR);
}
public DivButton(Context context, int id, int style) {
super(context,null);
this.buttonID = id;
init(style);
}
public DivButton(Context context, int id){
super(context,null);
this.buttonID = id;
init(DEFAULT);
}
private void init(int style){
radian = DEFAULT_RADIAN;
initRadian();
if(PICTURE != style){
if(mDrawable == null){
mDrawable = getShapeDrawable(style);
}
this.getBackground().setAlpha(0);
this.setTextColor(Color.WHITE);
}
this.setOnTouchListener(this);
}
public DivButton(Context context, int id, AttributeSet attrs) {
super(context,attrs);
this.buttonID = id;
init(DEFAULT);
}
public DivButton(Context context, int id, ShapeDrawable mDrawable){
super(context);
this.buttonID = id;
this.mDrawable = mDrawable;
}
public int getCmdId() {
return buttonID;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(mDrawable != null){
mDrawable.setBounds(0, 0, this.getWidth(), this.getHeight());
if(!isPressed){
mDrawable.getPaint().setShader(getNormalColor(this.getWidth(), this.getHeight()));
}else{
mDrawable.getPaint().setShader(getPressedColor(this.getWidth(), this.getHeight()));
}
//mDrawable.getPaint().setColor(Color.BLUE);
//mDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
mDrawable.draw(canvas);
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(getTextSize());
paint.setColor(Color.WHITE);
FontMetrics fm = paint.getFontMetrics();
int y = getTop() + (int)(getHeight() - fm.ascent)/2;
canvas.drawText((String)getText(), getWidth()/2, y, paint);
}
public void onDown() {
onDefaultDown();
if(mOnChooseListener != null){
mOnChooseListener.onDown();
}
}
public void onUp() {
onDefaultUp();
if(mOnChooseListener != null){
mOnChooseListener.onUp();
}
}
public void onDefaultUp(){
isPressed = false;
invalidate();
}
public void onDefaultDown(){
isPressed = true;
invalidate();
}
public void changeButtonStyle(int style){
getShapeDrawable(style);
invalidate();
}
private ShapeDrawable getShapeDrawable(int style){
this.style = style;
if(style == TAB){
mDrawable = new ShapeDrawable(new RoundRectShape(TAB_OUTRADII, null,
null));
}else if(style == SEGMENT_LEFT){
mDrawable = new ShapeDrawable(new RoundRectShape(LEFT_OUTRADII, null,
null));
}else if(style == SEGMENT_CENTER){
mDrawable = new ShapeDrawable(new RoundRectShape(CENTER_OUTRADII, null,
null));
}else if(style == SEGMENT_RIGHT){
mDrawable = new ShapeDrawable(new RoundRectShape(RIGHT_OUTRADII, null,
null));
}else{
mDrawable = new ShapeDrawable(new RoundRectShape(DEFAULT_OUTRADII, null,
null));
}
return mDrawable;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!isPressed) {
if(hasPressedDrawable()){
setBackgroundResource(pressedDrawableId);
}
// 更改为按下时的背景图
onDown();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (isPressed) {
if(hasDefaultDrawable()){
setBackgroundResource(defaultDrawableId);
}
// 改为抬起时的图片
onUp();
}
}
// TODO Auto-generated method stub
return false;
}
public interface OnChooseListener{
public void onDown();
public void onUp();
}
}
main.xml 写道
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.qiamian.test.SegmentControl
android:id="@+id/segcontrol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
<!-- android:gravity="right" -->
<ViewFlipper android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.qiamian.test.SegmentControl
android:id="@+id/segcontrol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
<!-- android:gravity="right" -->
<ViewFlipper android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/qm_text1" android:gravity="center" android:textSize="36px" android:text="www.juapk.com TEST_A" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/qm_text1" android:gravity="center" android:textSize="36px" android:text="juapk.com Demo-TEST_B" /> </LinearLayout>
交流请加 Android 群 : 137824028
发表评论
-
android反编译工具(ApkDec-Release-0.1)-正式版
2013-03-30 22:54 1655ApkDec是一款免费的绿色APK反编译工具 forand ... -
Nexus One变卫星 HTC与NASA打造太空计划
2012-10-13 15:15 955Nexus One变卫星 HTC与NASA打造太空计划 ... -
Android 模仿酷狗播放器(精品源码)
2012-10-10 22:20 943Android 模仿酷狗播放器精品Dem ... -
Google发布App开发准则 为自主Android平板开路
2012-10-10 12:45 906虽然Nexus 7的发布在Android平板市场 ... -
Android安卓视频教程(历史最全)
2012-10-09 21:47 1702Android开发视频教程第一季(1-5) http: ... -
Android安卓41个开源项目
2012-10-09 21:34 13941、Android安卓推送框 ... -
Android安卓51个开源代码
2012-10-09 21:21 18461、Android植物大战僵尸(源码) http:// ... -
Android开发者必须深入的UI组件和10个应用开源项目
2012-09-21 13:23 2388学习开源代码是掌握技术的一个最佳方式。一下为网络总结 ... -
android开发资源汇总
2012-09-10 17:38 1034android开发资源汇总 Android ... -
Android开发视频教程汇总
2012-09-09 13:47 987Android开发视频教程汇总 Andro ...
相关推荐
Android 开发—使用 ActivityGroup 来切换 Activity 和 Layout Android 开发中,经常需要在不同的 Activity 之间进行切换,并且显示不同的ContentView。为了实现这个功能,我们可以使用 ActivityGroup 来管理不同...
在Android开发中,ActivityGroup是一种特殊类型的Activity,它允许开发者在一个Activity内嵌套其他Activity,实现类似TabHost或Fragment的效果,但这是在Android早期版本(API级别较低)中的一种实现方式。...
ActivityGroup允许你在同一个父Activity中启动多个子Activity,这样在切换Tab时,可以保持每个Tab的状态,避免了重复加载和资源浪费。通过在ActivityGroup中使用LocalActivityManager,我们可以管理和控制这些子...
`ActivityGroup`是`TabActivity`的一种替代方案,它允许我们在一个父`Activity`中嵌套多个子`Activity`,并在需要时动态替换内容。 首先,我们需要了解`ActivityGroup`的核心概念。`ActivityGroup`是`Sherlock`库中...
Fragment可以在一个Activity中动态地添加、移除和替换,实现了类似ActivityGroup的功能,但更加灵活和轻量。 - ViewPager配合FragmentPagerAdapter或FragmentStatePagerAdapter可以轻松实现Tab分页效果,而且更容易...
在Android开发中,ActivityGroup是一种特殊的Activity容器,它允许在一个Activity内部嵌套多个子Activity,形成类似多级菜单或者Tab效果。本示例"android ActivityGroup的demo"将深入探讨如何使用ActivityGroup来...
这通常通过自定义的启动方法实现,如`startChildActivity()`,在该方法中,你需要管理Activity的生命周期,并将新Activity的视图添加到ActivityGroup的布局中。 Activity外跳转则是指从ActivityGroup中的子Activity...
2. **LayoutInflator与ViewStub**:为了在ActivityGroup中添加子Activity,可能需要利用LayoutInflater将布局文件动态地加载到当前Activity的视图层次结构中。同时,ViewStub可能是用来延迟加载子Activity布局的工具...
`ActivityGroup`是早期Android版本(Android 2.x)提供的一种特殊类型的`ViewGroup`,允许在同一个`TabHost`或`FrameLayout`中嵌套显示多个Activity,以实现类似多级菜单或者子窗口的效果。但是,由于在Android 3.0...
3. ActivityGroup的使用:在ActivityGroup中,开发者通常会重写onCreateView()和onDestroyView()方法,以便在需要时动态地添加或移除子Activity的视图。这种方式可以让多个Activity在同一个窗口内切换,而不需要每次...
Fragment可以像Activity一样拥有自己的UI和生命周期,而且可以在同一个Activity中进行动态添加、删除和替换,解决了ActivityGroup的一些问题,同时也更符合Android设计原则。 在升级到使用Fragment时,需要注意以下...
这通常涉及到如何启动新的子Activity并将其添加到当前的ActivityGroup中。这个过程通常需要重写`startChildActivity()`方法,以便在ActivityGroup内部管理子Activity的生命周期。例如: ```java public void ...
在早期的Android版本中,ActivityGroup用于在一个Tab内管理多个Activity的堆栈。它的主要作用是在不离开TabHost的情况下,实现内部Activity的切换和堆栈管理。然而,从Android 3.0(API级别11)开始,ActivityGroup...
在Android应用开发中,ActivityGroup是一种特殊的容器,它允许在一个栈结构中管理多个子Activity,常见于早期Android版本(低于Android 3.0)的Tab布局或者自定义导航栏设计中。ActivityGroup的主要目的是实现类似...
在Android开发中,ActivityGroup是一种老式的容器组件,它允许开发者在一个Activity中嵌套其他Activity作为子视图。然而,自Android 3.0(API级别11)开始,这个功能已被Fragment替代,因此现在使用ActivityGroup并...
ActivityGroup的用法,头部和底部为固定布局中间动态显示界面 并在中间布局实现手势滑动效果,自定义SlidingDrawer(抽屉)效果,在ActivityGroup里执行跳转等功能。至于在Android在ActivityGroup里执行跳转详细介绍 ...
ActivityGroup则是在早期版本的Android系统中用来管理多个Activity在同一个父Activity内部进行切换的技术,它允许在一个View里展示多个Activity,从而实现类似Tab的效果。 **TabHost详解:** TabHost是Android SDK...
- 在自定义的ActivityGroup中重写onCreate()方法,设置布局并添加子Activity。 - 使用LocalActivityManager管理子Activity,通过startChildActivity()方法启动子Activity。 - 实现onSaveInstanceState()和...
这可能是自定义的`ActivityGroup`(旧版Android中的概念),或者一个处理Tab切换时显示不同文本内容的类。在现代Android开发中,我们通常使用`Fragment`来替代`ActivityGroup`,并结合`ViewPager`实现页面滑动和Tab...
2. **startChildActivity()**:这是一个自定义方法,用于启动并添加子Activity到当前的ActivityGroup中。你需要提供子Activity的Intent,并通过LocalActivityManager的startActivity()方法启动它。 3. **...