SlidingDrawer抽屉效果 |
|
|
<?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"
>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="100px"
android:background="#0000ff">
</LinearLayout>
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:handle="@+id/slidingDrawerButton"
android:content="@+id/content"
android:background="#ffffff">
<Button
android:id="@+id/slidingDrawerButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
package com.magus.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class SlidingDrawer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.widget.SlidingDrawer mDrawer;
final LinearLayout mLinearLayout;
mDrawer = (android.widget.SlidingDrawer) findViewById(R.id.slidingDrawer);
mDrawer.open();
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
mDrawer
.setOnDrawerOpenListener(new android.widget.SlidingDrawer.OnDrawerOpenListener() {
public void onDrawerOpened() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout
.getLayoutParams();
linearParams.height = 100;
mLinearLayout.setLayoutParams(linearParams);
}
});
mDrawer
.setOnDrawerCloseListener(new android.widget.SlidingDrawer.OnDrawerCloseListener() {
public void onDrawerClosed() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout
.getLayoutParams();
linearParams.height = 400;
mLinearLayout.setLayoutParams(linearParams);
}
});
mDrawer.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
}
|
图片的左右移动 |
|
|
布局文件:
<?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"
>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/list_bg"
/>
<Button
android:id="@+id/btn_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="前"
/>
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="后"
/>
</LinearLayout>
package com.magus.translate;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class App extends Activity {
private Button btn_0, btn_1;
private ImageView iv;
private int count;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.iv);
iv.bringToFront();
btn_0 = (Button) findViewById(R.id.btn_0);
btn_0.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TranslateAnimation animation = new TranslateAnimation(
count * 100, 100 + count * 100, 0, 0);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(400);
animation.setFillAfter(true);
iv.startAnimation(animation);
count++;
}
});
btn_1 = (Button) findViewById(R.id.btn_1);
btn_1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TranslateAnimation animation = new TranslateAnimation(
count * 100, -100 + count * 100, 0, 0);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(400);
animation.setFillAfter(true);
iv.startAnimation(animation);
count--;
}
});
}
}
|
电视关闭效果 |
android |
|
package com.magus.tv;
import android.graphics.Matrix;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class TVOffAnimation extends Animation {
private int halfWidth;
private int halfHeight;
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
setDuration(500);
setFillAfter(true);
//保存View的中心点
halfWidth = width / 2;
halfHeight = height / 2;
setInterpolator(new AccelerateDecelerateInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final Matrix matrix = t.getMatrix();
if (interpolatedTime < 0.8) {
matrix.preScale(1+0.625f*interpolatedTime, 1-interpolatedTime/0.8f+0.01f,halfWidth,halfHeight);
}else{
matrix.preScale(7.5f*(1-interpolatedTime),0.01f,halfWidth,halfHeight);
}
}
}
package com.magus.tv;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView img = (ImageView) findViewById(R.id.imageView);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img.startAnimation(new TVOffAnimation());
}
});
}
}
|
自己定义的View |
view |
|
package com.android.tutor;
import com.magus.view2.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Paint mPaint;
private Context mContext;
private static final String mString = "Welcome to Mr yiWei's blog";
public MyView(Context context) {
super(context);
mPaint = new Paint();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF);
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/*mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStyle(Style.FILL);*/
mPaint.setStyle(Style.FILL);
canvas.drawRect(10, 10, 100, 100, mPaint);
mPaint.setColor(Color.BLUE);
canvas.drawText(mString, 10, 120, mPaint);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
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"
/>
<com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
package com.magus.view2;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
|
有道词典和全局变量 |
android |
|
布局文件:
<?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"
>
<!-- 建立一個EditText -->
<EditText
android:id="@+id/myEditText1"
android:layout_width="200px"
android:layout_height="40px"
android:textSize="18sp"
android:layout_x="5px"
android:layout_y="32px"
/>
<!-- 建立一個Button -->
<Button
android:id="@+id/myButton01"
android:layout_width="60px"
android:layout_height="40px"
android:text="查询"
android:layout_x="205px"
android:layout_y="35px"
/>
<Button
android:id="@+id/myButton02"
android:layout_height="40px"
android:layout_width="50px"
android:text="清空"
android:layout_y="35px"
android:layout_x="270px"
/>
<!-- 建立一個WebView -->
<WebView
android:id="@+id/myWebView1"
android:layout_height="330px"
android:layout_width="300px"
android:layout_x="7px"
android:layout_y="90px"
android:focusable="false"
/>
</LinearLayout>
package com.magus.youdao;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
// 查询按钮申明
private Button myButton01;
// 清空按钮申明
private Button myButton02;
// 输入框申明
private EditText mEditText1;
// 加载数据的WebView申明
private WebView mWebView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int code = getVerCode(getApplicationContext());
String name = getVerName(getApplicationContext());
((Test) getApplication()).setSum(100);
System.out.println("code=" + code);
System.out.println("name=" + name);
System.out.println("sum=" + ((Test) getApplication()).getSum());
// 获得布局的几个控件
myButton01 = (Button) findViewById(R.id.myButton01);
myButton02 = (Button) findViewById(R.id.myButton02);
mEditText1 = (EditText) findViewById(R.id.myEditText1);
mWebView1 = (WebView) findViewById(R.id.myWebView1);
// 查询按钮添加事件
myButton01.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
String strURI = (mEditText1.getText().toString());
strURI = strURI.trim();
// 如果查询内容为空提示
if (strURI.length() == 0) {
Toast.makeText(getApplicationContext(), "查询内容不能为空!",
Toast.LENGTH_LONG).show();
}
// 否则则以参数的形式从http://dict.youdao.com/m取得数据,加载到WebView里.
else {
String strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q="
+ strURI;
mWebView1.loadUrl(strURL);
}
}
});
// 清空按钮添加事件,将EditText置空
myButton02.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mEditText1.setText("");
}
});
}
public static int getVerCode(Context context) {
int verCode = -1;
try {
verCode = context.getPackageManager().getPackageInfo(
"com.magus.youdao", 0).versionCode;
} catch (NameNotFoundException e) {
}
return verCode;
}
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo(
"com.magus.youdao", 0).versionName;
} catch (NameNotFoundException e) {
}
return verName;
}
}
设置全局变量的方法:
package com.magus.youdao;
import android.app.Application;
public class Test extends Application {
public int sum;
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
}
|