记录那些经常用却又打死记不住的代码(持续更新)

Posted by alonealice on 2017-08-02

时间固定格式转Long

1
2
3
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(style);
Date date = simpleDateFormat.parse(time);
return date.getTime();

组合属性动画

方法1:

1
2
3
4
5
PropertyValuesHolder holder1=
PropertyValuesHolder.ofFloat("scaleX",1.0f,1,5f);
...
ObjectAnimator animator=ObjectAnimator.
ofPropertyValuesHolder(view,holder1,holder2,holder3);

方法2:

1
2
3
4
AnimatorSet set=new AnimatorSet();
AnimatorSet.Builder builder=set.play(animator1);
builder.after(animator2);
set.start();

方法3:

1
2
3
4
5
6
7
8
9
10
 AnimatorInflater.loadAnimator(context,R.animator.t);
动画配置文件:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:ordering="together">
<objectAnimator
android:propertyName="translationX"></objectAnimator>
<objectAnimator
android:propertyName="translationY"></objectAnimator>
</set>

DOM解析xml

1
2
3
4
5
6
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder=builderFactory.newDocumentBuilder();
Document document= documentBuilder.parse(inputStream);
Element rootNode=document.getDocumentElement();
NodeList nodeList=rootNode.getChildNodes();
...

PULL解析

1
2
3
4
5
6
7
8
XmlResourceParser xmlResourceParser= getResources().getLayout(id);
AttributeSet attrs=Xml.asAttributeSet(xmlResourceParser);
while(XmlPullParser.END_DOCUMENT!=xmlResourceParser.getEventType()){
if(xmlResourceParser.getEventType()==XmlPullParser.START_TAG&&xmlResourceParser.getName().equals("item")){//条件满足,解析具体的值
int a=obtainStyledAttributes(attrs, R.styleable.MenuItem).getInt(R.styleable.CircleImage2_d3,0);
}
XmlPullParser.next();
}

SAX解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
SAXParserFactory sax=SAXParserFactory.newInstance();
InputStream inputStream=getResources().openRawResource(R.raw.sax);
SAXParser saxParser= sax.newSAXParser();
saxParser.parse(inputStream,new DefaultHandler(){
@Override
public void startDocument() throws SAXException {
super.startDocument();
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}

@Override
public void endDocument() throws SAXException {
super.endDocument();
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
}
});

其中characters是解析具体的数据时的回调,在startDocument、startElement和endElement方法执行。