woaimoliha4 发表于 2013-1-30 04:03:52

android仿苹果Iphone桌面源码

package com.cctvp.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import com.cctvp.R;
import com.cctvp.ui.view.AppInfo;
import com.cctvp.ui.view.AppView;
import com.cctvp.ui.view.ClipImageView;
import com.cctvp.ui.view.DragLayer;
import com.cctvp.ui.view.EventCenter;
import com.cctvp.ui.view.MyUtils;
import com.cctvp.ui.view.Workspace;
import com.cctvp.ui.view.WorkspaceAdapter;
import com.cctvp.ui.view.AppView.OnAppViewChangeListener;
import com.cctvp.ui.view.AppView.OnDeleteClickListener;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemClickListener;

/** 全部源码看附件
public class mainAimplements OnClickListener, Observe
ViewSwitcher {

      
    int[] img={
R.drawable.bbtc3,R.drawable.bbtc3,R.drawable.bbtc3
};
   
    BaseAdapter baseAdapter=new BaseAdapter(){
@Override
public int getCount() {
return img.length;
}
@Override
public Object getItem(int arg0) {   
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

ImageView imageView = new ImageView(activity);
//imageView.setImageResource(img);
imageView.setBackgroundResource(img);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,250));
return imageView;
}      
    };

//    private AppDataBO appDataBO = AppDataBO.getInstance();

    private AppViewListener appViewClickListener = new AppViewListener();
   
    private TextView txtCurUser;
   
    private int resourceId = 0;

    public PortalHome(Activity activity, View content) {
    Log.i("tag", "PortalHome类*****"+"PortalHome方法");
      this.activity = activity;
      this.content = content;
      init();
    }

    private void init() {

    Log.i("tag", "PortalHome类*****"+"init方法");

      this.switcher = (ViewSwitcher) content.findViewById(R.id.portal_home_switcher);//两个视图只显示一个,滑瓶
//      txtCurUser = (TextView)content.findViewById(R.id.cur_user_lable);
      if(txtCurUser != null){
//      User user = MosseUtil.getInstance().getCurrentUser();
//      if(user != null){
////      txtCurUser.setText(user.getUserName());
       //txtCurUser.setText("廉宇华");
//      }
      }
//      portalAddToDesktop = new PortalAddToDesktop(activity, content, this, this);//点击添加按钮后,显示添加视图


//      user_state_select = (ImageView) content.findViewById(R.id.user_state_select);
//      user_state = (ImageView) content.findViewById(R.id.user_state);
//      setUserStateImage(MosseUtil.getInstance().getCurrentUser().getLoginType());
      dragLayer = (DragLayer) content.findViewById(R.id.draglayer);//整个桌面的总布局
      workspace = (Workspace) content.findViewById(R.id.workspace);
      clipImageView = (ClipImageView) content.findViewById(R.id.clipview);//实现了WorkspaceListener这个接口
      eventCenter = new EventCenter(activity, workspace, dragLayer);
      dragLayer.setInterceptTouchEvent(eventCenter);
      
      adapter = new WorkspaceAdapter(activity, getUserHomeApps());

      // 设置4种事件,WorkspaceAdapter是被观察者同时设置个事件
      adapter.setOnClickListener(appViewClickListener);
      adapter.setOnLongClickListener(eventCenter);//间接:view.setOnLongClickListener
      adapter.setOnDeleteClickListener(appViewClickListener);
      adapter.setOnAppViewChangeListener(appViewClickListener);

      workspace.setWorkspaceAdapter(adapter);//设置适配器,同时设置了观察者
      workspace.setWorkspaceListener(clipImageView);
      
      adapter.notifyObservers();
      
      
      
      intiGallery();
      clipImageView.setGalleryView(gallery);
//      user_state_select.setOnClickListener(this);

    }
//    public void setUserStateImage(UserLoginType userLoginType){
//    if (userLoginType.equals(UserLoginType.LoginType_online)) {
//    user_state.setImageResource(R.drawable.user_state_select_online);
//}else {
//user_state.setImageResource(R.drawable.user_state_select_noline);
//}
//    }
   
   

   public void intiGallery(){
   gallery = (Gallery)content.findViewById(R.id.my_galley);
       gallery.setAdapter(baseAdapter);
   gallery.setOnItemClickListener(
    new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.i("tag", "onItemClick"+arg2);
gallery.setSelection(arg2);
}      
    }
    );
   }
   

    public Map<Integer, List<AppInfo>> getUserHomeApps() {
    MyUtils.saveImage(activity);
      HashMap<Integer, List<AppInfo>> apps = new HashMap<Integer, List<AppInfo>>();
      List<AppInfo> listAppInfo = new ArrayList<AppInfo>();
      List<AppInfo> listAppInfo2 = new ArrayList<AppInfo>();
      List<AppInfo> listAppInfo3 = new ArrayList<AppInfo>();
         for (int j = 1; j < 7; j++) {      
          AppInfoinfo = new AppInfo();
            info.app_id = j+"1";
                info.page_no =0;
                info.pos_no = j;
                if (info.app_id.equals("1")) {
                info.app_name = "lgf";
}else {
info.app_name = "lyh";
}
               
                if (info.app_id.equals("41")) {
                info.icon_file_name = "/data/data/com.cctvp/icon/1.png";
}else {
info.icon_file_name = "/data/data/com.cctvp/icon/2.png";
}
               
                info.app_state = 0;
                if (j==1||j==2) {
info.default_app = 1;
}else {
info.default_app = 0;
}
               
                info.version = 1;
                listAppInfo.add(info);
                Log.i("tag", "AppDataBO"+"info.app_id="+info.app_id);
                Log.i("tag", "AppDataBO"+"icon_file_name="+info.icon_file_name);
         }
         for (int j = 1; j < 7; j++) {      
         AppInfoinfo = new AppInfo();
         info.app_id = j+"2";
               info.page_no =1;
               info.pos_no = j;
               if (info.app_id.equals("1")) {
               info.app_name = "lgf";
}else if (info.app_id.equals("3")) {
info.app_name = "lyh";
}else {
info.app_name = "lyh2";
}
               
               if (info.app_id.equals("52")) {
               info.icon_file_name = "/data/data/com.cctvp/icon/2.png";
}else {
info.icon_file_name = "/data/data/com.cctvp/icon/1.png";
// Bitmap bitmap = BitmapFactory.decodeFile("/data/data/com.cctvp/icon/2.png");
// bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.football);
// Log.i("tag", "jhjh=="+bitmap);
}
            
               info.app_state = 0;
               if (j==1) {
info.default_app = 0;
}else {
info.default_app = 0;
}
               
               info.version = 1;
               listAppInfo2.add(info);
               Log.i("tag", "AppDataBO"+"info.app_id="+info.app_id);
               Log.i("tag", "AppDataBO"+"icon_file_name="+info.icon_file_name);
      }
         //第三页
         for (int j = 1; j < 7; j++) {      
         AppInfoinfo = new AppInfo();
         info.app_id = j+"3";
               info.page_no =2;
               info.pos_no = j;
               if (info.app_id.equals("1")) {
               info.app_name = "lgf";
}else {
info.app_name = "lyh";
}
               
               if (info.app_id.equals("13")) {
               info.icon_file_name = "/data/data/com.cctvp/icon/2.png";
}else {
info.icon_file_name = "/data/data/com.cctvp/icon/1.png";
}
            
               info.app_state = 0;
               if (j==1||j==2) {
info.default_app = 0;
}else {
info.default_app = 0;
}
               
               info.version = 1;
            listAppInfo3.add(info);
               Log.i("tag", "AppDataBO"+"info.app_id="+info.app_id);
               Log.i("tag", "AppDataBO"+"icon_file_name="+info.icon_file_name);
      }
         apps.put(0, listAppInfo);
//         listAppInfo.remove(3);
         apps.put(1, listAppInfo2);
         apps.put(2, listAppInfo3);
         return apps;
    }
    public void refreshWorkspace(){
    Log.i("tag", "PortalHome类*****"+"refreshWorkspace方法");
    if(null != adapter){
//    adapter.setApps(appDataBO.getUserHomeApps());
    adapter.notifyObservers();
    }
    }
   
    public void onAppInfoChange(String app_id ,int state) {
    Log.i("tag", "PortalHome类*****"+"onAppInfoChange方法");
    getWorkspace().changeApp(app_id , state);//更新桌面
    if(isShowAdd){
    //如果正在显示添加应用,则更新
//    portalAddToDesktop.onAppInfoChange(app_id , state);
    }
}

    public Workspace getWorkspace() {
      return workspace;
    }

    /*
   * 选择登录方式
   */
    private void selectLoginState() {
//      Intent intent = new Intent(activity, UserStateSelectActivity.class);
//      activity.startActivityForResult(intent, SELECE_STATE);
    }

    @Override
    public void onClick(View v) {
//      switch (v.getId()) {
//      case R.id.user_state_select:
//            selectLoginState();
//            break;
//
//      default:
//            break;
//      }
    }

    /**
   * 观察当用户切换状态时返回的状态值
   */
    @Override
    public void update(Observable o, Object arg) {
      if (arg instanceof Intent) {
            final Intent intent = (Intent) arg;
            if (intent.getIntExtra("select", 0) > 0 && null != user_state_select) {
                user_state.setImageResource(intent.getIntExtra("select", 0));// 设置在线状态
//                if (intent.getIntExtra("select", 0)==R.drawable.user_state_select_unline) {
//activity.finish();
//
//}
            }
      }
    }

    /**
   * 桌面图标点击事件处理
   *
   * @author Administrator
   *
   */
    private final class AppViewListener implements OnClickListener, OnDeleteClickListener, OnAppViewChangeListener {

      @Override
      public void onClick(View view) {
            if (view instanceof AppView && null != view) {
                final AppView appView = (AppView) view;
                if (null != appView.getAppInfo()) {
                  if (appView.getAppInfo().isAddApp()) {
                        if (!workspace.isLockWorkspace()) {// 如果workspace在锁定状态下是不可添加的
                            // 调用添加应用
                        showAddToDesk();
                        }
                  } else {
                        // 调用其它启动程序方法
                  new EventCenter.OnAppViewClick(activity).onClick(view);
                  }
                }
            }
      }

      @Override
      public void onDeleteClick(AppView view) {
            Utils.createDeleteDailog(activity, workspace, view.getAppInfo());
      }

      @Override
      public void onAppViewChange(AppView view) {
      Log.i("tag", "PortalHome类*****"+"onAppViewChange方法");
            if (null != view && null != view.getAppInfo()) {
                final AppInfo appInfo = view.getAppInfo();
//                AppDataBO.getInstance().updateHomeApp(appInfo.app_id, appInfo.page_no, appInfo.pos_no);
            }
      }

    }

//    @Override
//    public boolean onAddDesk(List<AppInfo> appInfos) {//PortalAddToDesktop的内部接口OnAddDesk 给PortalAddToDesktop用
////      workspace.addNewAppView(appInfos);
////      return true;
//    }

@Override
public void showDesk() {//PortalHomeViewSwitcher给PortalAddToDesktop用
Log.i("tag", "PortalHome类*****"+"showDesk方法");
final int i = switcher.getDisplayedChild();
if(i == 1){
switcher.showPrevious();
isShowAdd = false;
}

}
/**
* 显示添加应用到首页
*/
@Override
public void showAddToDesk() {//PortalHomeViewSwitcher 给PortalAddToDesktop用
Log.i("tag", "PortalHome类*****"+"showAddToDesk方法");
final int i = switcher.getDisplayedChild();
if(i == 0){
switcher.showNext();
//portalAddToDesktop.loadData();
isShowAdd = true;
}

}

   
}
interface PortalHomeViewSwitcher{

void showDesk();

void showAddToDesk();

}
页: [1]
查看完整版本: android仿苹果Iphone桌面源码