类似于桌面启动器




我们基本上都见识过iphone的桌面启动器,我觉得很不错哦!
然后这次自己在做Android应用的时候,我就想做一个类似于那样的桌面启动器,然后就到处在网上看看,然后 总算弄了一个三栽版的:
下面啊的一个java类,基本上的动画以及走动都在里面了!


package
cn.android.app; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.GradientDrawable.Orientation; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.View.OnTouchListener; import android.widget.AbsoluteLayout; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; /** * * 这个项目,我做一做,看能不能完成,就是得到控件的坐标,然后移动 * */ public class DotRotateActivity extends Activity { /** Called when the activity is first created. */ private Button btn; /* 声明相关变量作为存储图片位置使用 */ private int intDefaultX, intDefaultY; private float mX, mY; /* 声明存储屏幕的分辨率变量 */ private int intScreenX, intScreenY; // 这里我来得到控件 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.btn); /* 取得屏幕对象 */ DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); /* 取得屏幕解析像素 */ intScreenX = dm.widthPixels; intScreenY = dm.heightPixels; // 调用背景颜色 drawBackground(); btn.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { System.out.println(" 这里是对按钮有效。。。look it...."); // 调用这个方法,我们可以还原小球的位置 // restoreBtn(); return false; } }); } // 实现点击事件 // 定义一个变量,然后来统计次数 private int count; public void onAction(View v) { switch (v.getId()) { case R.id.btn: count++; LinearLayout menu = (LinearLayout) findViewById(R.id.menu); if (count % 2 == 0) { System.out.println("原来在点击了两次哦!"); menu.setVisibility(View.GONE); } else { System.out.println("呵呵,我只是单击了单次!"); menu.setVisibility(View.VISIBLE); } // 得到gridview的实例 myGridView = (GridView) menu.findViewById(R.id.gridview); // 这里创建一个绑定适配器的方法 bindAdapter(); break; case R.id.btn_two: System.out.println("dot me..."); break; } } private GridView myGridView; private void bindAdapter() { MyCarAdapter adapter = new MyCarAdapter(); myGridView.setAdapter(adapter); } // 自定义一个adapter,可以灵活的来控制gridview子控件的布局显示 // 这是一个内部类 class MyCarAdapter extends BaseAdapter { // 图片的索引id int[] img = { R.drawable.emoji_000, R.drawable.emoji_001, R.drawable.emoji_002, R.drawable.emoji_003 }; // 再定义一个数组,然后与之对应 String[] name = { "one", "two", "three", "four" }; // 这里我们最好是重新写一下构造方法 public MyCarAdapter() { } // 获取条目的数量 @Override public int getCount() { // TODO Auto-generated method stub return img.length; } // 返回子控件的内容 @Override public Object getItem(int position) { // TODO Auto-generated method stub return img[position]; } // 获取子控件的id @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } // 重新加载view的方法 @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView = null; if (convertView == null) { // 通过layoutinflater类加载布局,返回一个view的实例对象 convertView = LayoutInflater.from(DotRotateActivity.this) .inflate(R.layout.main_view, null); } // 通过convertview调用findviewbyid方法根据其id返回相应的子控技 imageView = (ImageView) convertView.findViewById(R.id.image); imageView.setImageResource(img[position]); TextView textview = (TextView) convertView .findViewById(R.id.text_name); textview.setText(name[position]); return convertView; } } // 覆盖触控事件 @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub /* 取得手指触控屏幕的位置 */ float x = ev.getX(); float y = ev.getY(); try { /* 触控事件的处理 */ switch (ev.getAction()) { /* 点击屏幕 */ case MotionEvent.ACTION_DOWN: picMove(x, y); break; /* 移动位置 */ case MotionEvent.ACTION_MOVE: picMove(x, y); break; /* 离开屏幕 */ case MotionEvent.ACTION_UP: picMove(x, y); break; } } catch (Exception e) { // TODO: handle exception } return super.onTouchEvent(ev); } // // 还原btn的位置处理 // private void restoreBtn() { // // intDefaultX = intScreenX - (btn.getWidth() / 2); // intDefaultY = intScreenY - (btn.getHeight() / 2); // System.out.println("this intDefaultX is " + intDefaultX // + "this intDefaultY is " + intDefaultY); // btn.setLayoutParams(new AbsoluteLayout.LayoutParams(btn.getWidth(), btn // .getHeight(), intDefaultX, intDefaultY)); // // } // 这里我们添加一个移动的方法 private void picMove(float x, float y) { // 默认微调图片与指针的相对位置 mX = x - (btn.getWidth() / 2); mY = y - (btn.getHeight() / 2); if (mX + (btn.getWidth()) > intScreenX) { mX = intScreenX - btn.getWidth(); } else if (mX < 0) { mX = 0; } System.out.println("this mx is " + mX + "this my is " + mY); btn.setLayoutParams(new AbsoluteLayout.LayoutParams(btn.getWidth(), btn .getHeight(), (int) mX, (int) mY)); } // 加载背景颜色 public void drawBackground() { GradientDrawable grad = new GradientDrawable(Orientation.TL_BR, new int[] { Color.rgb(0, 0, 127), Color.rgb(0, 0, 255), Color.rgb(127, 0, 255), Color.rgb(0, 127, 255), Color.rgb(127, 255, 255), Color.rgb(255, 127, 255) }); this.getWindow().setBackgroundDrawable(grad); }
}


当然,这里也会有布局文件
第一个,就是点击按钮出来的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:paddingBottom="2dip"
    android:layout_marginLeft="60dip"
    android:paddingTop="2dip"
    android:visibility="gone" >

    
    <GridView
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dip"
        android:numColumns="2"
        android:verticalSpacing="15dip" />

</LinearLayout>

然后还有主页的布局:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ball"
        android:onClick="onAction"
        />
        <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dot me"
        android:onClick="onAction"
        />
    
    <RelativeLayout
        android:id="@+id/layout_visible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dip"
        android:layout_marginTop="100dip" 
    >

        <include layout="@layout/diy_menu" />
    </RelativeLayout>

</AbsoluteLayout>

最后还有一个布局:这个布局是布置点击按钮出来的布局里面然后加载的adapter里面的内容布局:

<?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" >

    <!-- 在这个布局文件里面,我们主要是设置adapter里面的布局 -->

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dip" />

    <TextView
        android:id="@+id/text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="" />

</LinearLayout>

一切只是为了充实自己!!stay hungry and stay foolish!!
原文地址:https://www.cnblogs.com/Catherine-Brain/p/3606659.html