Android桌面壁纸

来源:互联网 发布:linux公社 ftp 编辑:程序博客网 时间:2024/05/01 09:15

继承ViewGroup的MyWorkspace类

public class MyWorkSpace extends ViewGroup{    private static final String TAG = "MyWorkSpace";        private Scroller mScroller;        private VelocityTracker mVelocityTracker;        private int mCurScreen;        private int mDefaultScreen = 0;        private static final int TOUCH_STATE_REST = 0;        private static final int TOUCH_STATE_SCROLLING = 1;        private static final int SNAP_VELOCITY = 600;        private int mTouchState = TOUCH_STATE_REST;        private int mTouchSlop;        private float mLastMotionX;        private float mLastMotionY;        public Handler myHandler = new Handler()    {        public void handleMessage(Message msg)        {            int param1 = 0;            int width = MainActivity.tvCursor.getWidth();            if (msg.what == 0x123)            {                param1 = msg.arg1;            }                        TranslateAnimation ta = new TranslateAnimation(                    (param1 - 1) * width, param1 * width, 0, 0);            ta.setDuration(100);            ta.setFillEnabled(true);            ta.setFillAfter(true);            MainActivity.tvCursor.setAnimation(ta);        }            };        public MyWorkSpace(Context context, AttributeSet attrs)    {        this(context, attrs, 0);        // TODO Auto-generated constructor stub    }        public MyWorkSpace(Context context)    {        super(context);        // TODO Auto-generated constructor stub    }        public MyWorkSpace(Context context, AttributeSet attrs, int defStyle)    {        super(context, attrs, defStyle);        // TODO Auto-generated constructor stub        mScroller = new Scroller(context);        mCurScreen = mDefaultScreen;        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();            }        @Override    protected void onLayout(boolean changed, int l, int t, int r, int b)    {        // TODO Auto-generated method stub        if (changed)        {            int childLeft = 0;            final int childCount = getChildCount();                        for (int i = 0; i < childCount; i++)            {                final View childView = getChildAt(i);                if (childView.getVisibility() != View.GONE)                {                    final int childWidth = childView.getMeasuredWidth();                    childView.layout(childLeft,                            0,                            childLeft + childWidth,                            childView.getMeasuredHeight());                    childLeft += childWidth;                }            }        }    }        @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)    {        Log.e(TAG, "onMeasure");        super.onMeasure(widthMeasureSpec, heightMeasureSpec);                final int width = MeasureSpec.getSize(widthMeasureSpec);        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);        if (widthMode != MeasureSpec.EXACTLY)        {            throw new IllegalStateException(                    "ScrollLayout only canmCurScreen run at EXACTLY mode!");        }                final int heightMode = MeasureSpec.getMode(heightMeasureSpec);        if (heightMode != MeasureSpec.EXACTLY)        {            throw new IllegalStateException(                    "ScrollLayout only can run at EXACTLY mode!");        }                // The children are given the same width and height as the scrollLayout        final int count = getChildCount();        for (int i = 0; i < count; i++)        {            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);        }        // Log.e(TAG, "moving to screen "+mCurScreen);        scrollTo(mCurScreen * width, 0);    }        /**     * According to the position of current layout scroll to the destination     * page.     */    public void snapToDestination()    {        final int screenWidth = getWidth();        final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;        snapToScreen(destScreen);    }        public void snapToScreen(int whichScreen)    {        Log.e("", "snapToScreen============>" + whichScreen);        if (whichScreen < MainActivity.totalPageNum)        {            Message msg = new Message();            msg.what = 0x123;                        msg.arg1 = whichScreen;            myHandler.sendMessage(msg);        }        // get the valid layout page        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));        if (getScrollX() != (whichScreen * getWidth()))        {                        final int delta = whichScreen * getWidth() - getScrollX();            mScroller.startScroll(getScrollX(),                    0,                    delta,                    0,                    Math.abs(delta) * 2);            mCurScreen = whichScreen;            invalidate(); // Redraw the layout        }    }        public void setToScreen(int whichScreen)    {        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));        mCurScreen = whichScreen;        scrollTo(whichScreen * getWidth(), 0);    }        public int getCurScreen()    {        return mCurScreen;    }        @Override    public void computeScroll()    {        // TODO Auto-generated method stub        if (mScroller.computeScrollOffset())        {            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());            postInvalidate();        }    }        @Override    public boolean onTouchEvent(MotionEvent event)    {        // TODO Auto-generated method stub                if (mVelocityTracker == null)        {            mVelocityTracker = VelocityTracker.obtain();        }        mVelocityTracker.addMovement(event);                final int action = event.getAction();        final float x = event.getX();        final float y = event.getY();        switch (action)        {            case MotionEvent.ACTION_DOWN:                Log.e(TAG, "event down!");                if (!mScroller.isFinished())                {                    mScroller.abortAnimation();                }                mLastMotionX = x;                Log.e("", "============>x=" + x + ",y=" + y);                break;                        case MotionEvent.ACTION_MOVE:                int deltaX = (int)(mLastMotionX - x);                mLastMotionX = x;                                scrollBy(deltaX, 0);                break;                        case MotionEvent.ACTION_UP:                Log.e(TAG, "event : up");                // if (mTouchState == TOUCH_STATE_SCROLLING) {                final VelocityTracker velocityTracker = mVelocityTracker;                velocityTracker.computeCurrentVelocity(1000);                int velocityX = (int)velocityTracker.getXVelocity();                Log.e(TAG, "velocityX:" + velocityX);                                if (velocityX > SNAP_VELOCITY && mCurScreen > 0)                {                    // Fling enough to move left                    Log.e(TAG, "snap left");                    snapToScreen(mCurScreen - 1);                }                else if (velocityX < -SNAP_VELOCITY                        && mCurScreen < getChildCount() - 1)                {                    // Fling enough to move right                    Log.e(TAG, "snap right");                    snapToScreen(mCurScreen + 1);                }                else                {                    snapToDestination();                }                if (mVelocityTracker != null)                {                    mVelocityTracker.recycle();                    mVelocityTracker = null;                }                // }                mTouchState = TOUCH_STATE_REST;                break;            case MotionEvent.ACTION_CANCEL:                mTouchState = TOUCH_STATE_REST;                break;        }                return true;    }        @Override    public boolean onInterceptTouchEvent(MotionEvent ev)    {        // TODO Auto-generated method stub        Log.e(TAG, "onInterceptTouchEvent-slop:" + mTouchSlop);                final int action = ev.getAction();        if ((action == MotionEvent.ACTION_MOVE)                && (mTouchState != TOUCH_STATE_REST))        {            return true;        }                final float x = ev.getX();        final float y = ev.getY();                switch (action)        {            case MotionEvent.ACTION_MOVE:                final int xDiff = (int)Math.abs(mLastMotionX - x);                if (xDiff > mTouchSlop)                {                    mTouchState = TOUCH_STATE_SCROLLING;                                    }                break;                        case MotionEvent.ACTION_DOWN:                mLastMotionX = x;                mLastMotionY = y;                mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST                        : TOUCH_STATE_SCROLLING;                break;                        case MotionEvent.ACTION_CANCEL:            case MotionEvent.ACTION_UP:                mTouchState = TOUCH_STATE_REST;                break;        }                return mTouchState != TOUCH_STATE_REST;    }    }


MainActivity入口类

public class MainActivity extends Activity implements OnClickListener{    private LinearLayout mainlayout;        private RelativeLayout rl_main;        private ImageView btnCall = null;        private ImageView btnSms = null;        private ImageView btnContact = null;        private ImageView btnWebView = null;        private GridView gvApp1;        private GridView gvApp2;        private GridView gvApp3;        private GridView gvApp4;        private GridView gvApp5;        private com.huawei.ccs.MyWorkSpace myWorkSpace;        private List<AppInfo> data1 = new ArrayList<AppInfo>();        private List<AppInfo> data2 = new ArrayList<AppInfo>();        private List<AppInfo> data3 = new ArrayList<AppInfo>();        private List<AppInfo> data4 = new ArrayList<AppInfo>();        private List<AppInfo> data5 = new ArrayList<AppInfo>();        public static TextView tvCursor;        public LinearLayout ll_cursor;        private LinearLayout webViewLayout; //webView布局        private LinearLayout controllLayout; //底部控制条布局        private WebView currentWebView;        static int totalPageNum;        private static Map<Integer, LinearLayout> WebView_Map = new HashMap<Integer, LinearLayout>(); //存放WebView Map        private static int WebView_Index = 0; //WebView_Map key        private static final int BUTTON_BACK = 0;        private static final int BUTTON_FORWARD = 1;        private static final int BUTTON_REFRESH = 2;        private static final int BUTTON_HOME = 3;        private static final int BUTTON_OTHER = 4;        private static final int WEBVIEW_ID = 888;        private int[] icons = new int[] {R.drawable.a, R.drawable.b, R.drawable.c,            R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,            R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.a,            R.drawable.b, R.drawable.c, R.drawable.a, R.drawable.b,            R.drawable.c, R.drawable.a, R.drawable.b, R.drawable.c,            R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,            R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k,            R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k,            R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,            R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o,            R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k,            R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,            R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o,            R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k,            R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,            R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o,            R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k};        @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mainlayout = (LinearLayout)this.findViewById(R.id.MainLayout);        ll_cursor = (LinearLayout)findViewById(R.id.ll_cursor);        btnCall = (ImageView)findViewById(R.id.call_btn);        btnSms = (ImageView)findViewById(R.id.sms_btn);        btnContact = (ImageView)findViewById(R.id.contact_btn);        btnWebView = (ImageView)findViewById(R.id.menu_btn);        myWorkSpace = (com.huawei.ccs.MyWorkSpace)findViewById(R.id.workspace);        rl_main = (RelativeLayout)findViewById(R.id.rl_main);        gvApp1 = (GridView)findViewById(R.id.gv_app);        loadApps();        gvApp1.setAdapter(new AppAdapter(this, data1));        totalPageNum = 1;        if (icons.length > 12)        {            View view2 = LayoutInflater.from(this)                    .inflate(R.layout.app_gridview, null);            gvApp2 = (GridView)view2.findViewById(R.id.gv_app);            gvApp2.setAdapter(new AppAdapter(this, data2));            myWorkSpace.addView(view2);            totalPageNum++;        }        if (icons.length > 24)        {            View view3 = LayoutInflater.from(this)                    .inflate(R.layout.app_gridview, null);            gvApp3 = (GridView)view3.findViewById(R.id.gv_app);            gvApp3.setAdapter(new AppAdapter(this, data3));            myWorkSpace.addView(view3);            totalPageNum++;        }        if (icons.length > 36)        {            View view4 = LayoutInflater.from(this)                    .inflate(R.layout.app_gridview, null);            gvApp4 = (GridView)view4.findViewById(R.id.gv_app);            gvApp4.setAdapter(new AppAdapter(this, data4));            myWorkSpace.addView(view4);            totalPageNum++;        }        if (icons.length > 48)        {            View view5 = LayoutInflater.from(this)                    .inflate(R.layout.app_gridview, null);            gvApp5 = (GridView)view5.findViewById(R.id.gv_app);            gvApp5.setAdapter(new AppAdapter(this, data5));            myWorkSpace.addView(view5);            totalPageNum++;        }        int cursorWidth = 480 / totalPageNum;        tvCursor = new TextView(this);        tvCursor.setLayoutParams(new LayoutParams(cursorWidth, 2));        tvCursor.setBackgroundColor(Color.WHITE);        ll_cursor.removeAllViews();        ll_cursor.addView(tvCursor);                btnCall.setOnClickListener(this);        btnSms.setOnClickListener(this);        btnContact.setOnClickListener(this);        btnWebView.setOnClickListener(this);    }        public void loadApps()    {        AppInfo appInfo = null;        for (int i = 0; i < icons.length; i++)        {            appInfo = new AppInfo();            appInfo.setAppLabel("App" + (i + 1));            appInfo.setAppIcon(icons[i]);            if (i < 12)            {                data1.add(appInfo);            }            else if (i < 24)            {                data2.add(appInfo);            }            else if (i < 36)            {                data3.add(appInfo);            }            else if (i < 48)            {                data4.add(appInfo);            }            else if (i < 60)            {                data5.add(appInfo);            }        }    }        @Override    public void onClick(View v)    {        switch (v.getId())        {            //phone call            case R.id.call_btn:                Intent intent = new Intent(Intent.ACTION_DIAL);                startActivity(intent);                break;                        //send sms              case R.id.sms_btn:                Uri smsToUri = Uri.parse("smsto:");                Intent mIntent = new Intent(                        android.content.Intent.ACTION_SENDTO, smsToUri);                startActivity(mIntent);                break;                        //goto contact               case R.id.contact_btn:                Intent contactIntent = new Intent();                contactIntent.setAction(Intent.ACTION_VIEW);                contactIntent.setData(ContactsContract.Contacts.CONTENT_URI);                startActivity(contactIntent);                                break;                        //go cube             case R.id.menu_btn:                //                Intent intent1 = new Intent(MainActivity.this,                //                        MyWebViewControll.class);                //                startActivity(intent1);                                //生成WebView主布局                rl_main.setVisibility(View.GONE);                mainlayout.setVisibility(View.VISIBLE);                webViewLayout = new LinearLayout(this);                webViewLayout.setOrientation(LinearLayout.VERTICAL);                LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,                        LayoutParams.FILL_PARENT);                webViewLayout.setLayoutParams(lp);                                //生成WebView布局中的WebView                currentWebView = new WebView(this);                currentWebView.setId(WEBVIEW_ID);                LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT,                        700);                currentWebView.setLayoutParams(lp1);                currentWebView.loadUrl("www.baidu.com");                                //WebView主布局加载WebView                webViewLayout.addView(currentWebView);                //界面主布局加载ebView主布局                mainlayout.addView(webViewLayout);                //WebView主布局加载底部控制条                initControllView();                break;            case BUTTON_BACK:                if (currentWebView.canGoBack())                    currentWebView.goBack();                else                    Toast.makeText(MainActivity.this,                            "已经是最后一页了!!!",                            Toast.LENGTH_SHORT).show();                break;            case BUTTON_FORWARD:                if (currentWebView.canGoForward())                    currentWebView.goForward();                else                    Toast.makeText(MainActivity.this,                            "已经是第一页了!!!",                            Toast.LENGTH_SHORT).show();                break;            case BUTTON_REFRESH:                currentWebView.reload();                break;            case BUTTON_HOME:                toHome();                break;            case BUTTON_OTHER:                WebView_Map.put(WebView_Index, webViewLayout);                toWebViewById(WebView_Index);                break;            default:                break;                    }    }        //初始化控制条    private void initControllView()    {        controllLayout = new LinearLayout(this);        controllLayout.setOrientation(LinearLayout.HORIZONTAL);        LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, 100);        controllLayout.setLayoutParams(lp);        controllLayout.setPadding(15, 15, 0, 0);        createButton("QianJin", BUTTON_BACK);        createButton("HouTui", BUTTON_FORWARD);        createButton("ShuaXin", BUTTON_REFRESH);        createButton("ZhuYe", BUTTON_HOME);        createButton("DuoChuangKou", BUTTON_OTHER);                webViewLayout.addView(controllLayout);    }        private void createButton(String name, int bid)    {        Button btn = new Button(this);        btn.setText(name);        LayoutParams lp = new LayoutParams(90, 50);        btn.setLayoutParams(lp);        btn.setId(bid);        btn.setOnClickListener(this);        controllLayout.addView(btn);    }        //返回主页    private void toHome()    {        rl_main.setVisibility(View.VISIBLE);        mainlayout.setVisibility(View.GONE);        //        webViewLayout.setVisibility(View.GONE);        WebView_Index++;    }        //根据ID跳转WebView    public void toWebViewById(int index)    {        rl_main.setVisibility(View.GONE);        mainlayout.setVisibility(View.VISIBLE);        webViewLayout = WebView_Map.get(index);        currentWebView = (WebView)webViewLayout.findViewById(WEBVIEW_ID);        webViewLayout.setVisibility(View.VISIBLE);    }    }

AppAdapter.java

public class AppAdapter extends BaseAdapter{    private Context myContext;        private List<AppInfo> appInfo;        public AppAdapter(Context myContext, List<AppInfo> appInfo)    {        super();        this.myContext = myContext;        this.appInfo = appInfo;    }        public int getCount()    {        if (null != appInfo)        {            return appInfo.size();        }        return 0;    }        public AppInfo getItem(int position)    {        return appInfo.get(position);    }        public long getItemId(int position)    {        return position;    }        public View getView(int position, View convertView, ViewGroup parent)    {        View view = convertView;        MyHolder myholder = null;        if (null == view)        {                        view = LayoutInflater.from(myContext)                    .inflate(R.layout.grad_view_item, null);            myholder = new MyHolder();            myholder.tv = (TextView)view.findViewById(R.id.widget_label);            myholder.iv = (ImageView)view.findViewById(R.id.widget_icon);            view.setTag(myholder);        }        else        {            myholder = (MyHolder)view.getTag();        }        myholder.tv.setText(appInfo.get(position).getAppLabel());        myholder.iv.setImageResource(appInfo.get(position).getAppIcon());        return view;    }        private class MyHolder    {        ImageView iv;                TextView tv;            }}


AppInfo.java

public class AppInfo{    /**     * widget名字     */    private String appLabel;        /**     * widget图标id     */    private int appIcon;        public String getAppLabel()    {        return appLabel;    }        public void setAppLabel(String appLabel)    {        this.appLabel = appLabel;    }        public int getAppIcon()    {        return appIcon;    }        public void setAppIcon(int appIcon)    {        this.appIcon = appIcon;    }    }



 

主要布局文件:main.xml

<?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" >    <RelativeLayout        android:id="@+id/rl_main"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_x="0px"        android:layout_y="0px"        android:orientation="vertical" >        <com.huawei.ccs.MyWorkSpace            android:id="@+id/workspace"            android:layout_width="fill_parent"            android:layout_height="730px"            android:background="#000" >            <include                android:id="@+id/cell1"                layout="@layout/app_gridview" />        </com.huawei.ccs.MyWorkSpace>        <include            android:id="@+id/bottom_view"            layout="@layout/bottom" />        <LinearLayout            android:id="@+id/ll_cursor"            android:layout_width="480px"            android:layout_height="2px"            android:layout_above="@id/bottom_view"            android:background="#bbf" >        </LinearLayout>    </RelativeLayout>    <LinearLayout        android:id="@+id/MainLayout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_x="0px"        android:layout_y="0px"        android:background="#000"        android:orientation="vertical"        android:visibility="gone" >    </LinearLayout></AbsoluteLayout>


 bottom.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/bottom_layout"        android:layout_width="match_parent"        android:layout_height="85px"        android:layout_alignParentBottom="true"         android:background="#000"           android:gravity="center"        >        <ImageView            android:id="@+id/call_btn"            android:layout_width="52px"            android:layout_height="52px"            android:layout_marginLeft="54px"            android:layout_marginRight="54px"            android:src="@drawable/call" />        <ImageView            android:id="@+id/sms_btn"            android:layout_width="52px"            android:layout_height="52px"            android:layout_marginRight="55px"            android:src="@drawable/sms" />        <ImageView            android:id="@+id/contact_btn"            android:layout_width="52px"            android:layout_height="52px"            android:layout_marginRight="55px"            android:src="@drawable/contact" />        <ImageView            android:id="@+id/menu_btn"            android:layout_width="59px"            android:layout_height="59px"            android:layout_marginRight="54px"            android:src="@drawable/menu" />    </LinearLayout>

grad_view_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/widget_icon"        android:layout_width="90px"        android:layout_height="90px"        android:layout_marginBottom="5px"        android:layout_marginLeft="15px"        android:layout_marginRight="15px"        android:src="@drawable/a" />    <TextView        android:id="@+id/widget_label"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginBottom="15px"        android:text="finder"        android:textColor="#ffffff"        android:textSize="20px" /></LinearLayout>