当前位置: 编程技术>移动开发
本页文章导读:
▪ViewGroup用onLayout兑现view的自由移动 ViewGroup用onLayout实现view的自由移动
HelloViewGroup
package com.fxhy.stady.myviewGroup;
import com.fxhy.stady.myviewGroup.R;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.uti.........
▪ 切换Activity的非一般显示效果 切换Activity的特殊显示效果
private Button button1;
private Button button2;
private Button button3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//添.........
▪ Parcelable在Activity其间传递对象数组 Parcelable在Activity之间传递对象数组
1.首先你的对象需要被Parcelable
Person.java
public class Person implements Parcelable
{
private String name;
private String address;
public Person(String name, String address)
{
this.name =.........
[1]ViewGroup用onLayout兑现view的自由移动
来源: 互联网 发布时间: 2014-02-18
ViewGroup用onLayout实现view的自由移动
- HelloViewGroup
package com.fxhy.stady.myviewGroup; import com.fxhy.stady.myviewGroup.R; import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewManager; import android.widget.ImageView; public class HelloViewGroup extends ViewGroup { private float mTouchStartX; private float mTouchStartY; private int x = 0; private int y = 0; private int mSelectView = -1; Context mContext; public HelloViewGroup(Context context) { super(context); mContext = context; // TODO Auto-generated constructor stub } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (mSelectView != -1) { View v = getChildAt(mSelectView); if (v != null) v.layout(x, y, x + 300, y + 500); return; } View v = getChildAt(0); if (v != null) v.layout(x, y, x + 300, y + 500); View v1 = getChildAt(1); if (v1 != null) v1.layout(x + 300, y, x + 600, y + 500); View v2 = getChildAt(2); if (v2 != null) v2.layout(x + 600, y, x + 900, y + 500); } public boolean onTouchEvent(MotionEvent event) { float down_x = event.getX(); float down_y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (mSelectView == -1) { for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if ((child.getLeft() < down_x && down_x < child.getRight()) && (child.getTop() < down_y && down_y < child .getBottom())) { mSelectView = i; break; } } } break; case MotionEvent.ACTION_MOVE: x = (int) event.getRawX() - 100; y = (int) event.getRawY() - 100; this.requestLayout(); break; case MotionEvent.ACTION_UP: mSelectView = -1; break; } return true; } }
- MainActivity
package com.fxhy.stady.myviewGroup; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.AttributeSet; import android.util.Log; import android.widget.ImageView; public class MainActivity extends Activity { Context mContext; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); HelloViewGroup my = new HelloViewGroup(this); mContext = this; ImageView mIcon = new ImageView( mContext); ImageView mIcon1 = new ImageView( mContext); ImageView mIcon2 = new ImageView( mContext); mIcon.setImageResource(R.drawable.nvshen1); mIcon1.setImageResource(R.drawable.nvshen2); mIcon2.setImageResource(R.drawable.nvshen5); my.addView(mIcon); my.addView(mIcon1); my.addView(mIcon2); setContentView(my); } }
[2] 切换Activity的非一般显示效果
来源: 互联网 发布时间: 2014-02-18
切换Activity的特殊显示效果
private Button button1; private Button button2; private Button button3; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); //添加界面切换效果,注意只有Android的2.0(SdkVersion版本号为5)以后的版本才支持 int version = Integer.valueOf(android.os.Build.VERSION.SDK); if(version >= 5){ button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Intent intent = new Intent(ActivitySwitch.this , Main.class); startActivity(intent); //此处添加切换的特殊效果的代码 overridePendingTransition(R.anim.zoomin,R.anim.zoomout); } }); button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Intent intent = new Intent(ActivitySwitch.this , Main.class); startActivity(intent); //此处添加切换的特殊效果的代码 overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right); } }); button3 = (Button)findViewById(R.id.button3); button3.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Intent intent = new Intent(ActivitySwitch.this , Main.class); startActivity(intent); //此处添加切换的特殊效果的代码 overridePendingTransition(android.R.anim.fade_in , android.R.anim.fade_out); } }); } }
zoomin.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> <scale android:fromXScale="2.0" android:toXScale="1.0" android:fromYScale="2.0" android:toYScale="1.0" android:pivotX="50%p" android:pivotY="50%p" android:duration="@android:integer/config_mediumAnimTime" /> </set>
zoomout.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:zAdjustment="top"> <scale android:fromXScale="1.0" android:toXScale=".5" android:fromYScale="1.0" android:toYScale=".5" android:pivotX="50%p" android:pivotY="50%p" android:duration="@android:integer/config_mediumAnimTime" /> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="@android:integer/config_mediumAnimTime" /> </set>
[3] Parcelable在Activity其间传递对象数组
来源: 互联网 发布时间: 2014-02-18
Parcelable在Activity之间传递对象数组
1.首先你的对象需要被Parcelable
Person.java
public class Person implements Parcelable { private String name; private String address; public Person(String name, String address) { this.name = name; this.address = address; } public String getName() { return this.name; } public String getAddress() { return this.address; } /** 下面实现的是对象的序列化*/ public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(address); } public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { public Person createFromParcel(Parcel source) { Person person = new Person("",""); person.name = source.readString(); person.address = source.readString(); return person; } public Person[] newArray(int size) { return new Person[size]; } }; }
2.
传递
class BtnSeeLocationListener implements OnClickListener { public void onClick(View v) { Intent intent = new Intent(); intent.setClass(Main.this, MapViewActivity.class); Bundle bundle = new Bundle(); bundle.putParcelableArrayList(SER_KEY, personList); //这里不适用Bundle应该也可以,用下面的方法替代 //intent.putParcelableArrayListExtra(SER_KEY, personList); intent.putExtras(bundle); startActivity(intent); } }
3.接收
Intent intent = getIntent(); personList = (ArrayList<Person>)intent.getExtras().getSerializable(Main.SER_KEY);//接收对象
最新技术文章: