package com.tarena.day02_02;
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
public class Day02_02Activity extends Activity {
MyView myview1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(R.id.root);
myview1 = new MyView(this);
layout.addView(myview1);
myview1.setOnTouchListener(new Ontouch());
}
class Ontouch implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
myview1.x= event.getX();
myview1.y = event.getY();
//重繪組件
myview1.invalidate();
return true;
}
}
}
package com.tarena.day02_02;
import java.util.logging.LogRecord;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
public class Day02_02Activity extends Activity {
MyView myview1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(R.id.root);
myview1 = new MyView(this);
layout.addView(myview1);
final Handler h= new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what == 0x1122){
myview1.invalidate();
}
}
};
//匿名內部類
new Thread(){
int a=5,b=5;
public void run(){
while(true){
int width = myview1.getWidth();
int height = myview1.getHeight();
if(myview1.x>= width - 15) a=-a;
if(myview1.x<=0+15) a=-a;
myview1.x+=a;
if(myview1.y>=height-15)b=-b;
if(myview1.y<=0+15) b=-b;
myview1.y-=b;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message msg = new Message();
msg.what=0x1122;
h.sendMessage(msg);
}
}
}.start();
//myview1.setOnTouchListener(new Ontouch());
}
class Ontouch implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
myview1.x= event.getX();
myview1.y = event.getY();
//重繪組件
myview1.invalidate();
return true;
}
}
}
package com.tarena.day02;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Day02Activity extends Activity {
//靜態初始化
//int imgs[]; C++的寫法
int[] imgs ={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
int curInt = 0;
ImageView img,imgleft,imgright;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonleft = (Button )findViewById(R.id.button1);
buttonleft.setOnClickListener(new Clickleft());
Button buttonright = (Button )findViewById(R.id.button2);
buttonright.setOnClickListener(new Clickright());
LinearLayout layout =(LinearLayout)findViewById(R.id.root);
//在layout上添加組件
img= new ImageView(this);
layout.addView(img);
img.setImageResource(R.drawable.a);
/*imgleft= new ImageView(this);
layout.addView(imgleft);
imgleft.setImageResource(R.drawable.left);
imgleft.setOnClickListener(new Clickleft());
imgright= new ImageView(this);
layout.addView(imgright);
imgright.setImageResource(R.drawable.right);
imgright.setOnClickListener(new Clickleft()); */
}
class Clickleft implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(imgs[curInt%4]);
curInt--;
}
}
class Clickright implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(imgs[curInt%4]);
curInt++;
}
}
}
-----------------------------------------------------------------
也可實現點擊圖片,進行翻頁
====================
4、佈局
1\漸變色
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="210px"
android:height="60px"
android:background="#ff0000"
android:id="@+id/t1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180px"
android:height="60px"
android:background="#ee0000"
android:id="@+id/t2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150px"
android:height="60px"
android:background="#dd0000"
android:id="@+id/t3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
android:height="60px"
android:background="#990000"
android:id="@+id/t4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="90px"
android:height="60px"
android:background="#770000"
android:id="@+id/t5"
/><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60px"
android:height="60px"
android:background="#550000"
android:id="@+id/t6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="30px"
android:height="60px"
android:background="#330000"
android:id="@+id/t7"
/>
</FrameLayout>
---
.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color7">#330000</color>
<color name="color6">#550000</color>
<color name="color5">#770000</color>
<color name="color4">#990000</color>
<color name="color3">#dd0000</color>
<color name="color2">#ee0000</color>
<color name="color1">#ff0000</color>
</resources>
-----
.java
package com.tarena.day0203;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class Day02_03Activity extends Activity {
int[] colors = {R.color.color7,R.color.color6,R.color.color5,R.color.color4,R.color.color3,R.color.color2,R.color.color1};
int curInt=0;
int[] tId = {R.id.t1,R.id.t2,R.id.t3,R.id.t4,R.id.t5,R.id.t6,R.id.t7};
TextView[] ts = new TextView[7];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
for(int i=0;i<7;i++){
ts[i]=(TextView)findViewById(tId[i]);
}
final Handler h = new Handler(){
@Override
public void handleMessage(Message msg) {
for(int i=0;i<7-curInt;i++){
ts[i].setBackgroundResource(colors[i+curInt]);
}
}
};
new Thread(){
public void run() {
while(true){
curInt++;
if(curInt>=7){
curInt=0;
}
Message m = new Message();
m.what=0x1122;
h.sendMessage(m);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
實現效果爲:
2、格式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="2"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="單獨佔一行"
/>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按鈕"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可拉伸的按鈕按鈕"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可壓縮的按鈕按鈕"
/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1,2"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="單獨佔一行"
/>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按鈕"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可拉伸的按鈕按鈕"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可壓縮的按鈕按鈕"
/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:collapseColumns="1"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="單獨佔一行"
/>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕一"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可按鈕二"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕三"
/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="單獨佔一行"
/>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕一"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可按鈕二"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕三"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕一"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可按鈕二"
/>
</TableRow>
</TableLayout>
</LinearLayout>
實現效果爲: