安卓学习37

今天学习了安卓的一些知识其中主要的是:

Android数组排序常见方法

122.

Android的数组排序方式基本上使用了Sun原生的Java API实现,常用的有Comparator接口实现compare方法和

Comparable接口的compareTo方法,我们对于一个数组列表比如

ArrayList可以通过这两个接口进行排序和比较,这里Android123给大家一个例子

123.

124.

private final Comparator cwjComp arator = new Comparator() { private final Collator collator = Colla tor.getInstance();

125. public final int compare(Object a, Object b) {

126. CharSequence a = ((Item) a).sName;

127. CharSequence b = ((Item) b).sID;

128. return http://www.doczj.com/doc/ca10c5fc6037ee06eff9aef8941ea76e58fa4a08.htmlpare(a, b);

129. }

130. };

Android系统的进度条控件默认的设计的不是很周全,比如没有包含文字的显示,那么如何在Android进度条控件上显示文字呢? 来自Google内部的代码来了解下,主要使用的addView这样的方法通过覆盖一层Chronometer秒表控件来实现,整个代码如下

135.

public class TextProgressBar ext ends RelativeLayout implements OnChronometerTickListener {

136. public static final String TAG = "TextProgressBar";

137. static final int CHRONOMETER_ID = android.R.id.text1; 138. static final int PROGRESSBAR_ID = android.R.id.progress; 139. Chronometer mChronometer = null;

140. ProgressBar mProgressBar = null;

141. long mDurationBase = -1;

142. int mDuration = -1; boolean mChronometerFollow = false; 143. int mChronometerGravity = Gravity.NO_GRAVITY;

144. public TextProgressBar(Context context, AttributeSet attrs, int d efStyle) {

145. super(context, attrs, defStyle);

146. } public TextProgressBar(Context context, AttributeSet attrs) {

147. super(context, attrs);

148. } public TextProgressBar(Context context) {

149. super(context);

150. } //Android开发网提示关键部分在这里 @Override

151. public void addView(View child, int index, http://www.doczj.com/doc/ca10c5fc6037ee06eff9aef8941ea76e58fa4a08.htmlyoutPar ams params) {

152. super.addView(child, index, params);

153. int childId = child.getId();

154. if (childId == CHRONOMETER_ID && child instanceof Chron ometer) {

155. mChronometer = (Chronometer) child;

156. mChronometer.setOnChronometerTickListener(this); 157. // Check if Chronometer should move with with ProgressBa r

158. mChronometerFollow = (params.width == http://www.doczj.com/doc/ca10c5fc6037ee06eff9aef8941ea76e58fa4a08.htmlyo utParams.WRAP_CONTENT);

159. mChronometerGravity = (mChronometer.getGravity() & Gr avity.HORIZONTAL_GRAVITY_MASK);

160. } else if (childId == PROGRESSBAR_ID && child instanceof ProgressBar) {

161. mProgressBar = (ProgressBar) child;

162. }

163. } @android.view.RemotableViewMethod

164. public void setDurationBase(long durationBase) {

165. mDurationBase = durationBase;

166. if (mProgressBar == null || mChronometer == null) {

167. throw new RuntimeException("Expecting child ProgressBar with id " +

168. "'android.R.id.progress' and Chronometer id 'android.

R.id.text1'");

169. }

170. // Update the ProgressBar maximum relative to Chronometer base

171. mDuration = (int) (durationBase - mChronometer.getBase()); 172. if (mDuration <= 0) {

173. mDuration = 1;

174. }

175. mProgressBar.setMax(mDuration);

176. }

177. public void onChronometerTick(Chronometer chronometer) { 178. if (mProgressBar == null) {

179. throw new RuntimeException(

180. "Expecting child ProgressBar with id 'android.R.id.progre ss'");

181. }

182. // Stop Chronometer if we're past duration

183. long now = SystemClock.elapsedRealtime();

184. if (now >= mDurationBase) {

185. mChronometer.stop();

186. } int remaining = (int) (mDurationBase - now);

187. mProgressBar.setProgress(mDuration - remaining);

188. if (mChronometerFollow) {

189. http://www.doczj.com/doc/ca10c5fc6037ee06eff9aef8941ea76e58fa4a08.htmlyoutParams params;

190. params = (http://www.doczj.com/doc/ca10c5fc6037ee06eff9aef8941ea76e58fa4a08.htmlyoutParams) mProgressBar.g etLayoutParams();

191. int contentWidth = mProgressBar.getWidth() - (params.left Margin + params.rightMargin);

192. int leadingEdge = ((contentWidth * mProgressBar.getProgr ess()) /

193. mProgressBar.getMax()) + params.leftMargin; 194. int adjustLeft = 0;

195. int textWidth = mChronometer.getWidth();

196. if (mChronometerGravity == Gravity.RIGHT) {

197. adjustLeft = -textWidth;

198. } else if (mChronometerGravity == Gravity.CENTER_HORI ZONTAL) {

199. adjustLeft = -(textWidth / 2);

200. }

201. leadingEdge += adjustLeft;

202. int rightLimit = contentWidth - params.rightMargin - textWid th;

203. if (leadingEdge < params.leftMargin) {

204. leadingEdge = params.leftMargin;

205. } else if (leadingEdge > rightLimit) {

206. leadingEdge = rightLimit;

207. }

208. params = (http://www.doczj.com/doc/ca10c5fc6037ee06eff9aef8941ea76e58fa4a08.htmlyoutParams) mChronometer.

getLayoutParams();

209. params.leftMargin = leadingEdge;

210. mChronometer.requestLayout();

211. }

212. }

213. }

214.

原文地址:https://www.cnblogs.com/092e/p/14916663.html