9.18工作日誌,系統定位,自定義viewgroup,放射獲取注入的類

来源:互联网 发布:软件测试方法和技术 编辑:程序博客网 时间:2024/05/22 17:00

自寫工具類,更新activeandroid框架的數據庫

@Table(name = "CategoryData",id = "id")public class CategoryData extends Model implements Serializable{    @Column(name = "cid",index = true)    public int cid;    @Column(name = "namebn",index = true)    public String namebn;    @Column(name = "nameen",index = true)    public String nameen;    @Column(name = "namegn",index = true)    public String namegn;    public CategoryData()    {        super();    }}

使用到放射判斷這個類是不是使用了Table注入

 Annotation[] annotations = cls.getDeclaredAnnotations();            if (annotations != null) {                for (Annotation annotation : annotations) {                    String annName = annotation.toString();                    if (annName.indexOf("com.activeandroid.annotation.Table") != 0) {                        isActivDB = true;                        break;                    }                }            }

1.為什麼要用:annotation.toString();
因為如果使用annotation.getClass().getName(),getCanonicalName都是返回@XX(一個無效的名字),所以使用toString

參考
http://www.java2s.com/Tutorials/Java/java.lang.reflect/Field/0160__Field.getDeclaredAnnotations_.htm

Annotation[] annotations = field.getDeclaredAnnotations();annotations.toString()得到這樣的結果@com.activeandroid.annotation.Column(index=false, indexGroups=[], length=-1, name=orderbn, notNull=false, onDelete=NO_ACTION, onNullConflict=FAIL, onUniqueConflict=FAIL, onUniqueConflicts=[], onUpdate=NO_ACTION, unique=false, uniqueGroups=[])

對此寫了個有趣的正則取值代碼:

/**     * 获取放射的属性的值     * @param annotation     * @param annName     * @return     */    private String getVlaueByColumnAnnotation(Annotation annotation,String annName) {        String ret = "";        String pattern = annName + "=(.*?),";        Pattern r = Pattern.compile(pattern);        Matcher m = r.matcher(annotation.toString());        if (m.find()){            ret = m.group();            ret = ret.substring(annName.length()+1, ret.length()-1);        }        return ret;    }

Log.d(TAG,getVlaueByColumnAnnotation(annotation,”name”));
name>>>cid

將代碼放上jcenter:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0623/3097.html

寫了一個ViewGroup控件:

/** * 自動平鋪沒邊界viewgroup * Created by george.yang on 2015/9/15. */public class NoScllViewGroup extends ViewGroup {    private static final int PADDING_HOR = 10;//水平方向padding    private static final int PADDING_VERTICAL = 5;//垂直方向padding    private static final int SIDE_MARGIN = 5;//左右间距    private static final int TEXT_MARGIN = 5;//??    public NoScllViewGroup(Context context) {        super(context);    }    public NoScllViewGroup(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public NoScllViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context);    }    private void init (Context context) {    }    public void setTexts (List<String> strings) {        this.removeAllViews();        for (String str:strings) {            TextView tv  = new TextView(getContext());            tv.setText(str);            this.addView(tv);        }        this.requestLayout();    }    public void setImages (Activity activity,List<String> urls) {        this.removeAllViews();        LayoutInflater mlayoutInflater = (LayoutInflater) getContext()                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        for (int i=0;i<urls.size();i++) {            final  int index = i;            View view  = mlayoutInflater.inflate(R.layout.thumbitems,null);            SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.thumbimage);            simpleDraweeView.setImageURI(Uri.parse(urls.get(i)));            LayoutParams lp = simpleDraweeView.getLayoutParams();            int width = DensityUtil.getWidthWithScreenByPersent(activity,1f/4) - 5 * PADDING_HOR;            lp.width = width;            lp.height = width;            simpleDraweeView.setLayoutParams(lp);            this.addView(view);            view.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    if (listener!=null) {                        listener.onItemClick(null,v,index,0);                    }                }            });        }        this.requestLayout();    }    private AdapterView.OnItemClickListener listener;    public void setOnItemClickListener (AdapterView.OnItemClickListener listener) {        this.listener = listener;    }    public void setAdapter (Adapter adapter) {        this.removeAllViews();        int count = adapter.getCount();        for (int i=0;i<count;i++) {            final  int index = i;            View view = adapter.getView(i, null, null);            this.addView(view);            view.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    if (listener != null) {                        listener.onItemClick(null, v, index, 0);                    }                }            });        }        this.requestLayout();    }//    @Override//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//        measureChildren(widthMeasureSpec, heightMeasureSpec);//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);//    }////    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {//        int mViewGroupWidth  = getMeasuredWidth();  //当前ViewGroup的总宽度//        int mPainterPosX = left;  //当前绘图光标横坐标位置//        int mPainterPosY = top;  //当前绘图光标纵坐标位置////        int childCount = getChildCount();//        for ( int i = 0; i < childCount; i++ ) {////            View childView = getChildAt(i);////            int width  = childView.getMeasuredWidth();//            int height = childView.getMeasuredHeight();////            //如果剩余的空间不够,则移到下一行开始位置//            if( mPainterPosX + width > mViewGroupWidth ) {//                mPainterPosX = left;//                mPainterPosY += height;//            }////            //执行ChildView的绘制//            childView.layout(mPainterPosX,mPainterPosY,mPainterPosX+width, mPainterPosY+height);////            //记录当前已经绘制到的横坐标位置//            mPainterPosX += width;//        }//    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        int childCount = getChildCount();        int autualWidth = r - l;        int x = 0;// 横坐标开始        int y = 0;//纵坐标开始        int rows = 1;        for(int i=0;i<childCount;i++){            View view = getChildAt(i);            int width = view.getMeasuredWidth();            int height = view.getMeasuredHeight();            x += width+TEXT_MARGIN;            if(x>autualWidth){                x = width;                rows++;            }            y = rows*(height+TEXT_MARGIN);            if(i==0){                view.layout(x-width-TEXT_MARGIN, y-height, x-TEXT_MARGIN, y);            }else{                view.layout(x-width, y-height, x, y);            }        }    };    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int x = 0;//横坐标        int y = 0;//纵坐标        int rows = 1;//总行数        int specWidth = MeasureSpec.getSize(widthMeasureSpec);        int actualWidth = specWidth - SIDE_MARGIN * 2;//实际宽度        int childCount = getChildCount();        for(int index = 0;index<childCount;index++){            View child = getChildAt(index);            child.setPadding(PADDING_HOR, PADDING_VERTICAL, PADDING_HOR, PADDING_VERTICAL);            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);            int width = child.getMeasuredWidth();            int height = child.getMeasuredHeight();            x += width+TEXT_MARGIN;            if (x > actualWidth){//换行                x = width;                rows++;            }            y = rows*(height+TEXT_MARGIN);        }        setMeasuredDimension(actualWidth, y);    }}

參考:
http://www.tuicool.com/articles/u2eEbe
http://ticktick.blog.51cto.com/823160/1542200/

Retrofit 2.0:有史以来最大的改进
http://www.tuicool.com/articles/BJjYFzv

android系統定位:

//                mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);//                //篩選最後的定位方式//                Criteria c = new Criteria();//                c.setAccuracy(Criteria.ACCURACY_FINE); //精度高//                c.setPowerRequirement(Criteria.POWER_LOW); //电量消耗低//                c.setAltitudeRequired(false); //不需要海拔//                c.setSpeedRequired(false); //不需要速度//                c.setCostAllowed(false); //不需要费用//                String provider = mLocationManager.getBestProvider(c, false); //false是指不管当前适配器是否可用//                //1分鐘、距離大於200米獲取一次//                mLocationManager.requestLocationUpdates(provider,60000, 200,mLocationListener);//                Location location = mLocationManager.getLastKnownLocation(provider);//                if (location!=null) {//                    lat = location.getLatitude();//                    lng = location.getLongitude();

強烈不建議使用,很多機型獲取失敗!
參考:
http://blog.csdn.net/dier4836/article/details/7351116

0 0