当前位置: 编程技术>移动开发
本页文章导读:
▪俭省电量—手机屏幕亮度调整 节省电量—手机屏幕亮度调整
手机屏幕亮度调整:有两种方法,第一种是自动调整,第二种是拉动显示
首先,获取系统管理
/***********************************************************************
*
* .........
▪ Java 七七大新功能预览 Java 7七大新功能预览
Java 7已经完成的7大新功能: 1 对集合类的语言支持; 2 自动资源管理; 3 改进的通用实例创建类型推断; 4 数字字面量下划线支持; .........
▪ 双色球的格局实现 双色球的布局实现
效果图:[img][/img]工程结构图:[img][/img]MainActivity:package com.zzl.shuangseqiu;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.v.........
[1]俭省电量—手机屏幕亮度调整
来源: 互联网 发布时间: 2014-02-18
节省电量—手机屏幕亮度调整
手机屏幕亮度调整:有两种方法,第一种是自动调整,第二种是拉动显示
首先,获取系统管理
/*********************************************************************** * * Copyright: 2011, BAINA Technologies Co. Ltd. * Classname: SystemManager.java * Author: yuewang * Description: TODO * History: * 1. Date: 2011-9-21 * Author: yuewang * Modifycation: create the class. * ***********************************************************************/ package com.TestBrightness.manager; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.net.Uri; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.text.format.Formatter; import android.view.WindowManager; /** * @author yuewang * */ public class SystemManager { private Context mContext; private static SystemManager sInstance; private SystemManager(final Context context) { mContext = context; } /** * Advice to invoke init in application. * * @param context * @return */ public static SystemManager init(final Context context) { if (null == sInstance) { sInstance = new SystemManager(context); } return sInstance; } /** * * @return */ public static SystemManager getInstance() { return sInstance; } /** * 判断是否开启了自动亮度调节 * * @param aContext * @return */ public boolean isAutoBrightness() { boolean automicBrightness = false; try { final ContentResolver resolver = mContext.getContentResolver(); automicBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch (final SettingNotFoundException e) { e.printStackTrace(); } return automicBrightness; } /** * 设置屏幕亮度,这会反映到真实屏幕上 * * @param activity * @param brightness */ public void setBrightness(final Activity activity, final int brightness) { final WindowManager.LayoutParams lp = activity.getWindow() .getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); activity.getWindow().setAttributes(lp); } /** * 保存亮度设置状态 * * @param resolver * @param brightness */ public void saveBrightness(final int brightness) { final ContentResolver resolver = mContext.getContentResolver(); final Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness); // resolver.registerContentObserver(uri, true, myContentObserver); resolver.notifyChange(uri, null); } /** * 开启亮度自动调节 * * @param activity */ public void startAutoBrightness() { final ContentResolver resolver = mContext.getContentResolver(); Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); final Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); resolver.notifyChange(uri, null); } /** * 停止自动亮度调节 * * @param activity */ public void stopAutoBrightness() { final ContentResolver resolver = mContext.getContentResolver(); Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); final Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); resolver.notifyChange(uri, null); } /** * 获取屏幕的亮度 * * @param activity * @return */ public int getScreenBrightness() { int nowBrightnessValue = 0; try { final ContentResolver resolver = mContext.getContentResolver(); nowBrightnessValue = android.provider.Settings.System.getInt( resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (final Exception e) { e.printStackTrace(); } return nowBrightnessValue; } /** * 保存亮度的显示模式 * * @return */ public void setBrightnessMode(int mode) { Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, mode); } /** * 获取亮度的显示模式 * * @return */ public int getBrightnessMode() { try { return Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); } catch (SettingNotFoundException e) { return Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } } public String formatMemorySize(final long memory) { return Formatter.formatFileSize(mContext, memory); } }
然后通过Activity显示出来
package com.TestBrightness; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.telephony.TelephonyManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import com.TestBrightness.manager.SystemManager; public class TestBrightness extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Button Start; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SystemManager.init(this); showBrightnessSettingDialog(); GetPhone(); } private void GetPhone(){ // 创建电话管理 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 获取手机号码 String phoneId = tm.getLine1Number(); Toast.makeText(this, phoneId+"", Toast.LENGTH_SHORT).show(); System.out.println("=============" + phoneId); } @Override public void onClick(View v) { showBrightnessSettingDialog(); } private int mOldBrightness; private int mCurrentBrightness = 0; private int mOldAutomatic; private boolean mAutomaticAvailable; /** * Brightness value for dim backlight */ private static final int BRIGHTNESS_DIM = 20; /** * Brightness value for fully on */ private static final int BRIGHTNESS_ON = 255; // Backlight range is from 0 - 255. Need to make sure that user // doesn't set the backlight to 0 and get stuck private static final int MINIMUM_BACKLIGHT = BRIGHTNESS_DIM + 10; private static final int MAXIMUM_BACKLIGHT = BRIGHTNESS_ON; private void showBrightnessSettingDialog() { final SystemManager systemManager = SystemManager.getInstance(); final Builder builder = new AlertDialog.Builder(this); final View view = getLayoutInflater().inflate(R.layout.brightness_view, null); // set brightness seekbar final SeekBar brightnessBar = (SeekBar) view .findViewById(R.id.brightness_bar); brightnessBar.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT); int brightness = systemManager.getScreenBrightness(); int process = brightness - MINIMUM_BACKLIGHT; if (process < 0) { process = 0; mOldBrightness = MINIMUM_BACKLIGHT; mCurrentBrightness = MINIMUM_BACKLIGHT; } else { mOldBrightness = brightness; mCurrentBrightness = brightness; } brightnessBar.setProgress(process); // set automatic available checkbox final CheckBox autoBrightness = (CheckBox) view .findViewById(R.id.auto_brightness); mOldAutomatic = systemManager.getBrightnessMode(); mAutomaticAvailable = systemManager.isAutoBrightness(); autoBrightness.setChecked(mAutomaticAvailable); if (mAutomaticAvailable) { brightnessBar.setVisibility(View.GONE); } else { brightnessBar.setVisibility(View.VISIBLE); } autoBrightness .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged( final CompoundButton buttonView, final boolean isChecked) { mAutomaticAvailable = isChecked; if (isChecked) { brightnessBar.setVisibility(View.GONE); systemManager.startAutoBrightness(); // systemManager.setBrightness(MainActivity.this, // systemManager.getScreenBrightness()); int process = systemManager.getScreenBrightness() - MINIMUM_BACKLIGHT; if (process < 0) { process = 0; }// end if brightnessBar.setProgress(process); } else { brightnessBar.setVisibility(View.VISIBLE); systemManager.stopAutoBrightness(); // systemManager.setBrightness(MainActivity.this, // systemManager.getScreenBrightness()); int process = systemManager.getScreenBrightness() - MINIMUM_BACKLIGHT; if (process < 0) { process = 0; }// end if brightnessBar.setProgress(process); } } }); brightnessBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(final SeekBar seekBar) { } @Override public void onStartTrackingTouch(final SeekBar seekBar) { } @Override public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) { mCurrentBrightness = progress + MINIMUM_BACKLIGHT; systemManager.setBrightness(TestBrightness.this, mCurrentBrightness); } }); builder.setTitle(R.string.brightness); builder.setView(view); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int which) { // set brightness if (mAutomaticAvailable) { systemManager.saveBrightness(systemManager .getScreenBrightness()); } else { systemManager.saveBrightness(mCurrentBrightness); } } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int which) { // recover brightness systemManager.setBrightness(TestBrightness.this, mOldBrightness); systemManager.saveBrightness(mOldBrightness); // recover automatic brightness mode systemManager.setBrightnessMode(mOldAutomatic); } }); builder.show(); } }
brightness.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dip" android:src="/blog_article/@drawable/btn_auto_brightness_default/index.html" > </ImageView> <CheckBox android:id="@+id/auto_brightness" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginTop="10dip" android:text="@string/automatic_brightness" android:textColor="@android:color/white" > </CheckBox> <SeekBar android:id="@+id/brightness_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="20dip" > </SeekBar> </LinearLayout>
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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/brightness" android:onClick="onClick"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/brightness" android:id="@+id/Start"/> </LinearLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, TestBrightness!</string> <string name="app_name">TestBrightness</string> <string name="brightness">亮度</string> <string name="automatic_brightness">自动亮度</string> </resources>
[2] Java 七七大新功能预览
来源: 互联网 发布时间: 2014-02-18
Java 7七大新功能预览
Java 7已经完成的7大新功能:
1 对集合类的语言支持;
2 自动资源管理;
3 改进的通用实例创建类型推断;
4 数字字面量下划线支持;
5 switch中使用string;
6 二进制字面量;
7 简化可变参数方法调用。
下面我们来仔细看一下这7大新功能:
1 对集合类的语言支持
Java将包含对创建集合类的第一类语言支持。这意味着集合类的创建可以像Ruby和Perl那样了。
原本需要这样:
List<String> list = new ArrayList<String>();
list.add("item");
String item = list.get(0);
Set<String> set = new HashSet<String>();
set.add("item");
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key", 1);
int value = map.get("key");
现在你可以这样:
List<String> list = ["item"];
String item = list[0];
Set<String> set = {"item"};
Map<String, Integer> map = {"key" : 1};
int value = map["key"];
这些集合是不可变的。
2 自动资源管理
Java中某些资源是需要手动关闭的,如InputStream,Writes,Sockets,Sql classes等。这个新的语言特性允许try语句本身申请更多的资源,
这些资源作用于try代码块,并自动关闭。
这个:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
变成了这个:
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
你可以定义关闭多个资源:
try (
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest))
{
// code
}
为了支持这个行为,所有可关闭的类将被修改为可以实现一个Closable(可关闭的)接口。
3 增强的对通用实例创建(diamond)的类型推断
类型推断是一个特殊的烦恼,下面的代码:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
通过类型推断后变成:
Map<String, List<String>> anagrams = new HashMap<>();
这个<>被叫做diamond(钻石)运算符,这个运算符从引用的声明中推断类型。
4 数字字面量下划线支持
很长的数字可读性不好,在Java 7中可以使用下划线分隔长int以及long了,如:
int one_million = 1_000_000;
运算时先去除下划线,如:1_1 * 10 = 110,120 – 1_0 = 110
5 switch中使用string
以前你在switch中只能使用number或enum。现在你可以使用string了:
String s = ...
switch(s) {
case "quux":
processQuux(s);
// fall-through
case "foo":
case "bar":
processFooOrBar(s);
break;
case "baz":
processBaz(s);
// fall-through
default:
processDefault(s);
break;
}
6 二进制字面量
由于继承C语言,Java代码在传统上迫使程序员只能使用十进制,八进制或十六进制来表示数(numbers)。
由于很少的域是以bit导向的,这种限制可能导致错误。你现在可以使用0b前缀创建二进制字面量:
int binary = 0b1001_1001;
现在,你可以使用二进制字面量这种表示方式,并且使用非常简短的代码,可将二进制字符转换为数据类型,如在byte或short。
byte aByte = (byte)0b001;
short aShort = (short)0b010;
7 简化的可变参数调用
当程序员试图使用一个不可具体化的可变参数并调用一个*varargs* (可变)方法时,编辑器会生成一个“非安全操作”的警告。
JDK 7将警告从call转移到了方法声明(methord declaration)的过程中。这样API设计者就可以使用vararg,因为警告的数量大大减少了。
Java 7已经完成的7大新功能:
1 对集合类的语言支持;
2 自动资源管理;
3 改进的通用实例创建类型推断;
4 数字字面量下划线支持;
5 switch中使用string;
6 二进制字面量;
7 简化可变参数方法调用。
下面我们来仔细看一下这7大新功能:
1 对集合类的语言支持
Java将包含对创建集合类的第一类语言支持。这意味着集合类的创建可以像Ruby和Perl那样了。
原本需要这样:
List<String> list = new ArrayList<String>();
list.add("item");
String item = list.get(0);
Set<String> set = new HashSet<String>();
set.add("item");
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key", 1);
int value = map.get("key");
现在你可以这样:
List<String> list = ["item"];
String item = list[0];
Set<String> set = {"item"};
Map<String, Integer> map = {"key" : 1};
int value = map["key"];
这些集合是不可变的。
2 自动资源管理
Java中某些资源是需要手动关闭的,如InputStream,Writes,Sockets,Sql classes等。这个新的语言特性允许try语句本身申请更多的资源,
这些资源作用于try代码块,并自动关闭。
这个:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
变成了这个:
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
你可以定义关闭多个资源:
try (
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest))
{
// code
}
为了支持这个行为,所有可关闭的类将被修改为可以实现一个Closable(可关闭的)接口。
3 增强的对通用实例创建(diamond)的类型推断
类型推断是一个特殊的烦恼,下面的代码:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
通过类型推断后变成:
Map<String, List<String>> anagrams = new HashMap<>();
这个<>被叫做diamond(钻石)运算符,这个运算符从引用的声明中推断类型。
4 数字字面量下划线支持
很长的数字可读性不好,在Java 7中可以使用下划线分隔长int以及long了,如:
int one_million = 1_000_000;
运算时先去除下划线,如:1_1 * 10 = 110,120 – 1_0 = 110
5 switch中使用string
以前你在switch中只能使用number或enum。现在你可以使用string了:
String s = ...
switch(s) {
case "quux":
processQuux(s);
// fall-through
case "foo":
case "bar":
processFooOrBar(s);
break;
case "baz":
processBaz(s);
// fall-through
default:
processDefault(s);
break;
}
6 二进制字面量
由于继承C语言,Java代码在传统上迫使程序员只能使用十进制,八进制或十六进制来表示数(numbers)。
由于很少的域是以bit导向的,这种限制可能导致错误。你现在可以使用0b前缀创建二进制字面量:
int binary = 0b1001_1001;
现在,你可以使用二进制字面量这种表示方式,并且使用非常简短的代码,可将二进制字符转换为数据类型,如在byte或short。
byte aByte = (byte)0b001;
short aShort = (short)0b010;
7 简化的可变参数调用
当程序员试图使用一个不可具体化的可变参数并调用一个*varargs* (可变)方法时,编辑器会生成一个“非安全操作”的警告。
JDK 7将警告从call转移到了方法声明(methord declaration)的过程中。这样API设计者就可以使用vararg,因为警告的数量大大减少了。
[3] 双色球的格局实现
来源: 互联网 发布时间: 2014-02-18
双色球的布局实现
效果图:
[img]
[/img]
工程结构图:
[img]
[/img]
MainActivity:
LotteryView:
LotteryGbox:
LotteryComMethod:
LotteryBox:
效果图:
[img]
[/img]
工程结构图:
[img]
[/img]
MainActivity:
package com.zzl.shuangseqiu; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; public class MainActivity extends Activity implements LotteryComMethod, OnClickListener { /** * n 代表有几个选区,比如双色球,有红蓝两个选区,暂时未用到 s1代表第一个选区有几个数字, s2代表第二个选区有几个数字, * m1代表第一个选区至少选几个 m2代表第二个选区至少选几个 redlist用于存储选区1的球 bluelist用于存储选区2的球 * type表示了玩法,单注,复试,胆拖 */ private int n = 2; private LotteryBox[] lb; private LinearLayout ball; private int[] s = { 33, 16 }, m = { 6, 1 }, maxBall; private ArrayList<ArrayList<LotteryView>> ballList = new ArrayList<ArrayList<LotteryView>>(); private String[] name = {"1","2","3","4","5","6","7","8","9","10"}; boolean startZero = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ball = (LinearLayout) findViewById(R.id.ball); addBall(); } /** * 添加球 */ public void addBall() { lb = new LotteryBox[n]; for (int i = 0; i < n; i++) { lb[i] = new LotteryBox(this, null, "选择" + name[i]); lb[i].setPadding(10, 0, 0, 0); for (int j = 0; j < (startZero ? (s[i] + 1) : s[i]); j++) { LotteryView lv = new LotteryView(this, this, null, startZero ? j : j + 1, m[i]); lb[i].addBall(lv); } ball.addView(lb[i]); } } @Override public boolean check(LotteryView lv, boolean is) { // TODO Auto-generated method stub return false; } @Override public int checkMulti() { // TODO Auto-generated method stub return 0; } @Override public void clear() { // TODO Auto-generated method stub } @Override public int getDanTuoTotal() { // TODO Auto-generated method stub return 0; } @Override public int getFuShiTotal() { // TODO Auto-generated method stub return 0; } @Override public int getType() { // TODO Auto-generated method stub return 0; } @Override public void refesh() { // TODO Auto-generated method stub } @Override public void setType(int type) { // TODO Auto-generated method stub } @Override public void onClick(View arg0) { // TODO Auto-generated method stub } }
LotteryView:
/** * 彩票球类 */ package com.zzl.shuangseqiu; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; import android.widget.Button; public class LotteryView extends Button { /** * checked,unchecked,checned_dan分别为未选中,选中,胆拖状态 * single,fu_shi,dan_tuo分别为单注,复式,胆拖玩法 * status记录了该球的状态 * num记录了该球的号码 */ public final static int CHECKED = 0, UNCHECKED = 1, CHECKED_DAN = 2; public final static int SINGLE = 3, FU_SHI = 4, DAN_TUO = 5; private int status = UNCHECKED; private int num = -1; private boolean isdan = true; public boolean isIsdan() { return isdan; } public void setIsdan(boolean isdan) { this.isdan = isdan; } public LotteryView(final Context c,final LotteryComMethod context, AttributeSet attrs, int num,int max) { super(c, attrs); setBackgroundResource(R.drawable.white_ball); this.setNum(num); setPadding(0, 0, 0, 0); setText(String.valueOf(num)); setWidth(BitmapFactory.decodeResource(getResources(), R.drawable.white_ball).getWidth()); setHeight(BitmapFactory.decodeResource(getResources(), R.drawable.white_ball).getHeight()); setOnClickListener(new OnClickListener() { /** * 当球被点击时,监听状态,根据球当前的状态进行变化 未选中->选中 ->胆拖 * 并更新玩法状态 * 若第一次进入新状态,进行提示 */ public void onClick(View v) { if (status == CHECKED) { status = CHECKED_DAN; setBackgroundResource(R.drawable.twice_click); } else if (status == UNCHECKED && isdan) { status = CHECKED; setBackgroundResource(R.drawable.once_click); } else if(status == UNCHECKED && !isdan){ status = CHECKED_DAN; setBackgroundResource(R.drawable.twice_click); }else if (status == CHECKED_DAN) { status = UNCHECKED; setBackgroundResource(R.drawable.white_ball); } int ty=context.checkMulti(); context.check((LotteryView) v,true); context.setType(ty); context.refesh(); } }); } public void setStatus(int status) { if(status == UNCHECKED){ setBackgroundResource(R.drawable.white_ball); }else if(status == CHECKED){ setBackgroundResource(R.drawable.once_click); }else if(status == CHECKED_DAN){ setBackgroundResource(R.drawable.twice_click); } this.status = status; } public int getStatus() { return status; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } public void setNum(int num) { this.num = num; } public int getNum() { return num; } public void reSet(){ status = UNCHECKED; setBackgroundResource(R.drawable.white_ball); } public void reSetFu() { status = CHECKED; setBackgroundResource(R.drawable.blue_ball); } }
LotteryGbox:
package com.zzl.shuangseqiu; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.LinearLayout; public class LotteryGbox extends LinearLayout { private LinearLayout ll; private Context c; public LotteryGbox(Context context, AttributeSet attrs) { super(context, attrs); c = context; setOrientation(LinearLayout.VERTICAL); } @Override public void addView(View child) { if (ll == null) { ll = new LinearLayout(c); ll.setBackgroundResource(R.drawable.ball_under); ll.setOrientation(LinearLayout.HORIZONTAL); ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); ll.addView(child); super.addView(ll); } else { if (ll.getChildCount() == 10) { ll = new LinearLayout(c); ll.setBackgroundResource(R.drawable.ball_under); ll.setOrientation(LinearLayout.HORIZONTAL); ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); ll.addView(child); super.addView(ll); } else { ll.addView(child); } } } }
LotteryComMethod:
/** * * 该接口实现了每个需要用到lotteryView需要调用的方法,这些方法会在lotteryView中进行回调 * */ package com.zzl.shuangseqiu; public interface LotteryComMethod { void setType(int type); int getType(); int getFuShiTotal(); int getDanTuoTotal(); boolean check(LotteryView lv,boolean is); void clear(); void refesh(); int checkMulti(); }
LotteryBox:
package com.zzl.shuangseqiu; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.widget.LinearLayout; import android.widget.TextView; public class LotteryBox extends LinearLayout { private TextView title; private LotteryGbox content; public LotteryBox(Context context, AttributeSet attrs,String title) { super(context, attrs); this.title=new TextView(context); this.title.setText(title); this.title.setTextColor(Color.BLACK); this.title.setPadding(10, 0, 10, 0); content=new LotteryGbox(context, attrs); content.setBackgroundColor(0xffffff); addView(this.title); addView(content); setOrientation(LinearLayout.VERTICAL); } public void addBall(LotteryView lv){ content.addView(lv); } }
最新技术文章: