2011年12月18日 星期日
2011年12月6日 星期二
android appwidget使用thread (更新)
Clock_widget.java這個已經我改寫了,這才是我想要寫的thread ,不需要透過Handler,public static void start_service()這靜態方法讓thread直接呼叫來啟動service,
thread中有3個靜態方法set_second(int s) ,stop_thread(),start_thread()讓使用者來設定顯示時間間隔,停止,開始時間,其實我這project已經多加了許多功能(譬如鬧鐘,顯示方
式..),但是還不是我想要的.
滿有趣的我用了一個反向寫程式方法來啟動音樂,在Clock_widget.java結尾的地方(藍色部份)
其他程式部份我會在放上來(待續)
終於知道寫法犯了錯誤,下面的 public void onReceive(Context context, Intent intent)會啟動public Clock_widget()這個建構子,每當我按鈕,onReceive接收,就會呼叫public Clock_widget()這個建構子,因此thread會在產生,一直按就會一直產生thread,解決方法public void onUpdate才是初始化的地方,將public Clock_widget()拿掉,把內容放置onUpdate就OK了.
package test.Clock_widget1;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class CLOCK1_widget1_music_file {
private static File myFilePath;
private static List dirarray;
private static String filepath;
public CLOCK1_widget1_music_file(String path) {
//必須初始化array,否則error
}
//給路徑,取得all file list,給目錄或回上一層使用
public static List get_file_list(String path){
myFilePath = new File(path);
filepath=path;
//必須初始化array,否則error
dirarray=new ArrayList();
//加上第一項
dirarray.add("回到上一層");
//是目錄回傳list
if(myFilePath.isDirectory()){
for(int j=0;j
dirarray.add( myFilePath.list()[j]);
//Log.d("TAG F:",dirarray[j]);
}
}else{
//不是目錄回到上一層
//先回到當前目錄
filepath=myFilePath.getParent();
File f=new File(filepath);
//在一次就是上層目錄,但是若是根目錄除外
if(!filepath.equals("/")){
filepath=f.getParent();
}
myFilePath = new File(filepath );
for(int j=0;j
dirarray.add( myFilePath.list()[j]);
//Log.d("TAG F:",dirarray[j]);
}
}
return dirarray;
}
//當前路徑
public static String get_file_path(){
return filepath;
}
//選中file
public static String get_file(int i){
return dirarray.get(i);
}
public static boolean is_directory(String st){
File myFilePath = new File(st);
return myFilePath.isDirectory();
}
public static boolean is_file(String st){
File myFilePath = new File(st);
return myFilePath.isFile();
}
}
read more...
thread中有3個靜態方法set_second(int s) ,stop_thread(),start_thread()讓使用者來設定顯示時間間隔,停止,開始時間,其實我這project已經多加了許多功能(譬如鬧鐘,顯示方
式..),但是還不是我想要的.
滿有趣的我用了一個反向寫程式方法來啟動音樂,在Clock_widget.java結尾的地方(藍色部份)
其他程式部份我會在放上來(待續)
終於知道寫法犯了錯誤,下面的 public void onReceive(Context context, Intent intent)會啟動public Clock_widget()這個建構子,每當我按鈕,onReceive接收,就會呼叫public Clock_widget()這個建構子,因此thread會在產生,一直按就會一直產生thread,解決方法public void onUpdate才是初始化的地方,將public Clock_widget()拿掉,把內容放置onUpdate就OK了.
package test.Clock_widget1;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class CLOCK1_widget1_music_file {
private static File myFilePath;
private static List
private static String filepath;
public CLOCK1_widget1_music_file(String path) {
//必須初始化array,否則error
}
//給路徑,取得all file list,給目錄或回上一層使用
public static List
myFilePath = new File(path);
filepath=path;
//必須初始化array,否則error
dirarray=new ArrayList
//加上第一項
dirarray.add("回到上一層");
//是目錄回傳list
if(myFilePath.isDirectory()){
for(int j=0;j
dirarray.add( myFilePath.list()[j]);
//Log.d("TAG F:",dirarray[j]);
}
}else{
//不是目錄回到上一層
//先回到當前目錄
filepath=myFilePath.getParent();
File f=new File(filepath);
//在一次就是上層目錄,但是若是根目錄除外
if(!filepath.equals("/")){
filepath=f.getParent();
}
myFilePath = new File(filepath );
for(int j=0;j
dirarray.add( myFilePath.list()[j]);
//Log.d("TAG F:",dirarray[j]);
}
}
return dirarray;
}
//當前路徑
public static String get_file_path(){
return filepath;
}
//選中file
public static String get_file(int i){
return dirarray.get(i);
}
public static boolean is_directory(String st){
File myFilePath = new File(st);
return myFilePath.isDirectory();
}
public static boolean is_file(String st){
File myFilePath = new File(st);
return myFilePath.isFile();
}
}
read more...
2011年12月3日 星期六
TK的canvas元件,清除畫面
畫了3個圓
按清除,被清空畫布
使用set將圓形放入變數中
set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
就可使用delete來清除了
$obj delete $dc
範例:
#!/usr/bin/wish
#wm deiconify .
#wm title . "test"
#新建窗口
global circle
toplevel .main
wm title .main "Input String Field"
#指定窗口大小(400x400)和位置(60,60)
wm geometry .main 400x400+60+60; update
wm maxsize .main 1028 512
wm minsize .main 128 1
set ::c1 {}
set cx 100
set cy 100
set cr 50
#在窗口開一個frame
frame .main.frame1 -bd 2 -width 400 -height 300 -relief raised
place .main.frame1 -x 0 -y 0
#在frame建立畫布
canvas .main.frame1.canvas
place .main.frame1.canvas -x 0 -y 0
#在frame建立輸入框x
label .main.frame1.lx -text "X:"
place .main.frame1.lx -x 2 -y 270
entry .main.frame1.ex -width 5 -textvariable cx
place .main.frame1.ex -x 20 -y 270
#在frame建立輸入框y
label .main.frame1.ly -text "Y:"
place .main.frame1.ly -x 82 -y 270
entry .main.frame1.ey -width 5 -textvariable cy
place .main.frame1.ey -x 100 -y 270
#在frame建立輸入框r
label .main.frame1.lr -text "R:"
place .main.frame1.lr -x 162 -y 270
entry .main.frame1.er -width 5 -textvariable cr
place .main.frame1.er -x 180 -y 270
tk_optionMenu .main.frame1.to color red blue yellow green
place .main.frame1.to -x 260 -y 270
tk_optionMenu .main.frame1.tol line_width 1 2 3 4 5
place .main.frame1.tol -x 340 -y 270
#按鈕Draw
button .main.a -text "畫圓" -command { draw_circle .main.frame1.canvas $cx $cy $cr $line_width $color }
place .main.a -x 20 -y 360
#按鈕Close
button .main.a1 -text "清除" -command {
delete_circle .main.frame1.canvas $::circle
unset ::circle
#destroy .main
}
place .main.a1 -x 100 -y 360
#按鈕Close
button .main.a2 -text "關閉" -command {
destroy .main
}
place .main.a2 -x 200 -y 360
proc draw_circle {obj x y r line_width color } {
global node
set x0 [ expr $x - $r ]
set y0 [ expr $y - $r ]
set x1 [ expr $x + $r ]
set y1 [ expr $y + $r ]
set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
lappend ::circle $cir
puts $::circle
}
#將畫布上組件list清除
proc delete_circle {obj list_circle} {
foreach dc $list_circle {
$obj delete $dc
}
}
read more...
按清除,被清空畫布
使用set將圓形放入變數中
set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
就可使用delete來清除了
$obj delete $dc
範例:
#!/usr/bin/wish
#wm deiconify .
#wm title . "test"
#新建窗口
global circle
toplevel .main
wm title .main "Input String Field"
#指定窗口大小(400x400)和位置(60,60)
wm geometry .main 400x400+60+60; update
wm maxsize .main 1028 512
wm minsize .main 128 1
set ::c1 {}
set cx 100
set cy 100
set cr 50
#在窗口開一個frame
frame .main.frame1 -bd 2 -width 400 -height 300 -relief raised
place .main.frame1 -x 0 -y 0
#在frame建立畫布
canvas .main.frame1.canvas
place .main.frame1.canvas -x 0 -y 0
#在frame建立輸入框x
label .main.frame1.lx -text "X:"
place .main.frame1.lx -x 2 -y 270
entry .main.frame1.ex -width 5 -textvariable cx
place .main.frame1.ex -x 20 -y 270
#在frame建立輸入框y
label .main.frame1.ly -text "Y:"
place .main.frame1.ly -x 82 -y 270
entry .main.frame1.ey -width 5 -textvariable cy
place .main.frame1.ey -x 100 -y 270
#在frame建立輸入框r
label .main.frame1.lr -text "R:"
place .main.frame1.lr -x 162 -y 270
entry .main.frame1.er -width 5 -textvariable cr
place .main.frame1.er -x 180 -y 270
tk_optionMenu .main.frame1.to color red blue yellow green
place .main.frame1.to -x 260 -y 270
tk_optionMenu .main.frame1.tol line_width 1 2 3 4 5
place .main.frame1.tol -x 340 -y 270
#按鈕Draw
button .main.a -text "畫圓" -command { draw_circle .main.frame1.canvas $cx $cy $cr $line_width $color }
place .main.a -x 20 -y 360
#按鈕Close
button .main.a1 -text "清除" -command {
delete_circle .main.frame1.canvas $::circle
unset ::circle
#destroy .main
}
place .main.a1 -x 100 -y 360
#按鈕Close
button .main.a2 -text "關閉" -command {
destroy .main
}
place .main.a2 -x 200 -y 360
proc draw_circle {obj x y r line_width color } {
global node
set x0 [ expr $x - $r ]
set y0 [ expr $y - $r ]
set x1 [ expr $x + $r ]
set y1 [ expr $y + $r ]
set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
lappend ::circle $cir
puts $::circle
}
#將畫布上組件list清除
proc delete_circle {obj list_circle} {
foreach dc $list_circle {
$obj delete $dc
}
}
read more...
訂閱:
文章 (Atom)