Android_AsyncTaskDemo之QQ记步数(画圆形图片知识)
今天学习了AsyncTask Android 的异步机制。我简单的实现我的一个小小案例——qq记步数。然后穿插一个画圆形图片的知识点。
由于所学知识有限,目前我计数,还有排名等等我就简单的利用随机数实现。多有不是之处见谅啊。
我们的xml layout布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.qqsport.MainActivity" >
<!-- 头部 -->
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="...heyhhz...." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="45dp">
<!-- 附近排名 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView
android:id="@+id/scort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="附近排名" /> <TextView
android:id="@+id/fujin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="第0名" />
</LinearLayout>
<!-- 头像 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/face"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/face"/>
<TextView
android:id="@+id/bushu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
<!-- 排行榜 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:text="排行榜"/>
<TextView
android:id="@+id/place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第0名"
android:layout_marginTop="30dp"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
我们的MainActivity.class 文件 其中我们头像变成圆形的代码也在其中。
package com.example.qqsport; /**
* 1.写一个随机数充当 该用户的步数
* 2.实现 从1加到 该随机数的效果
*
*/ import java.util.Random; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends Activity {
private static final String TAG = "RoundImage";
private ImageView mImg;
private TextView tv_bushu,tv_place,tv_fujin;
private Random r = new Random();
private MyTask myTask = new MyTask(this); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
huatu();
int bushu = r.nextInt(5000)+5000;
//随机设置排名
int place = r.nextInt(50); //设置随机数 附近排名
int fujin = r.nextInt(200); //初始化步数
tv_bushu = (TextView) findViewById(R.id.bushu); //初始化 排行榜控件
tv_place = (TextView) findViewById(R.id.place);
//附近
tv_fujin = (TextView) findViewById(R.id.fujin);
myTask.execute(tv_bushu,bushu,tv_place,place,tv_fujin,fujin); } //画圆形图片
private void huatu() {
//初始化控件
mImg = (ImageView) findViewById(R.id.face);
//裁剪图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.face, options);
Log.d(TAG, "original outwidth:"+options.outWidth);
//此宽度是目标 imageView 希望的大小,你可以自定义imageView 然后获得ImageView 的宽度
int dstWidth = 100;
//我们需要加载的图片可能很大,我们先对原有的图片进行裁剪
int sampleSize = calculateInSampleSize(options, dstWidth, dstWidth);
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Log.d(TAG, "sample size:" + sampleSize);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.face, options);
//绘制图片
Bitmap resultBmp = Bitmap.createBitmap(dstWidth, dstWidth, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setAntiAlias(true);
Canvas canvas = new Canvas(resultBmp);
//画图
canvas.drawCircle(dstWidth / 2, dstWidth / 2, dstWidth / 2, paint);
//选择交集去上层图片
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getWidth()), new Rect(0, 0, dstWidth, dstWidth), paint);
mImg.setImageBitmap(resultBmp);
bmp.recycle();
} private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if( height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && ( halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
} } return inSampleSize;
} }
接下来是我们后台加载,异步机制。前面也说了,小编在这是利用随机数进行的,然后在这里加载出来的数据由于是同时传入的,他按照先后顺序进行显示,并没有实现同步的一个操作。这里要大家自己去实现下啦。
package com.example.qqsport; import android.app.Activity;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast; public class MyTask extends AsyncTask{ private Activity runActivit; private TextView tv_bs,tv_fj,tv_pla;
private int bs,pla,fj;
public MyTask (Activity activity){
this.runActivit = activity;
} @Override
protected Object doInBackground(Object... params) {
//我的步数
tv_bs = (TextView) params[0];
bs = (Integer) params[1];
for(int i = 1; i <= bs; i++) { publishProgress(i,1);
}
//排名
tv_pla = (TextView) params[2];
pla = (Integer) params[3];
for (int i = 1; i <= pla; i++){
publishProgress(i,2); }
//附近排名
tv_fj = (TextView) params[4];
fj = (Integer) params[5];
for (int i = 1; i <= fj; i++){
publishProgress(i,3); }
return "加载完成";
} //onPostExecute 后台数据结束后调用的方法
@Override
protected void onPostExecute(Object result) {
if( bs > 7000) {
Toast.makeText(runActivit, "哎哟,不错哟今天", 1).show();
}else {
Toast.makeText(runActivit, "偶尔放慢脚步可以思考人生", 1).show();
} } //onProgressUpdate 当前面使用了publishProgress 这个方法的时候就调用。
@Override
protected void onProgressUpdate(Object... values) {
Integer aa = (Integer) values[0];
Integer bb = (Integer) values[1];
if(bb == 1){
tv_bs.setText(aa + "");
}
if(bb == 2){ tv_pla.setText("第"+aa + "名");
}
if(bb == 3){ tv_fj.setText("第"+aa + "名");
} } }


大家可以拷贝代码自己试下。[微笑]欢迎大家交流指教,我也是初学者。
Android_AsyncTaskDemo之QQ记步数(画圆形图片知识)的更多相关文章
- 在android中画圆形图片的几种办法
在开发中常常会有一些需求,比方显示头像,显示一些特殊的需求,将图片显示成圆角或者圆形或者其它的一些形状. 可是往往我们手上的图片或者从server获取到的图片都是方形的.这时候就须要我们自己进行处理, ...
- android绘制圆形图片的两种方式
看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...
- iOS常见用户头像的圆形图片裁剪常见的几种方法
在开发中,基本上APP的用户头像的处理都需要把用户所上传的方形图片,处理为圆形图片.在这里就总结三种常见的处理圆形图片的方法. 1.使用位图上下文 2.使用UIView的layer进行处理 3.使用r ...
- Android实现圆形图片
情景再现: 写Android程序也有一段时间了,今天突然被问怎么实现一个圆形图片,很多app图像是圆形的.但是用户上传的图像可不是圆的,所以问题就来了,需要我们代码实现圆形图片.但是大脑飞转想到第三 ...
- UIImage类扩展返回一个带边框的圆形图片
/** * 将image转换为圆型带边框的图片(最好写一个UIImage的类扩展) * * @param name 图片的名字 * @param borderWidth 外层边框的宽度 * @para ...
- Android圆形图片--ImageView
[ RoundImageView.java ] package com.dxd.roundimageview; import android.content.Context; import andro ...
- iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画
CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...
- 使用CAShapeLayer来实现圆形图片加载动画[译]
原文链接 : How To Implement A Circular Image Loader Animation with CAShapeLayer 原文作者 : Rounak Jain 译文出自 ...
- Android圆形图片自定义控件
Android圆形图片控件效果图如下: 代码如下: RoundImageView.java package com.dxd.roundimageview; import android.content ...
随机推荐
- 独自handle一个数据库大程有感
这学期数据库课程,最后的大程是写一个MiniSQL的数据库实现,要求很简单,建删表,建删单值索引,支持主键和unique定义,支持最简单的select,只要支持3个类型:int,float,char( ...
- asp.net mvc多条件+分页查询解决方案
开发环境vs2010 css:bootstrap js:jquery bootstrap paginator 原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了 ...
- Sql Server Job 简单使用
http://www.cnblogs.com/zerocc/p/3400529.html(转载) use msdb EXEC sp_add_job @job_name = 'tk_bakdata' ...
- 解决java文件编码和windows7系统(中文版)默认编码冲突所导致的乱码情况
开篇从一个比较简单但是也比较蛋疼的问题开始吧. 背景介绍:我是新手小白,初学java. 问题介绍:在使用UTF-8编码格式写java文件时,编译出现问题. 原因分析:1.java文件的编码格式是UTF ...
- Python-0 简述
#1 应用广泛: 豆瓣 youtube 云存储相关 #2 初步学习内容:
- firebug常用工具
1.console.group("第一组"); console.log(1); console.groupend(); 2.console.dir(对象);//输出对象的所有信息 ...
- VI 命令 gg 跳到第一行,dG 删除后面的所有内容
VI 命令 gg 跳到第一行,dG 删除后面的所有内容
- AX7: Install a deployable package
Table of Contents Introduction Key concepts Collect topology configuration data Generate a runbook f ...
- 如何存session,取session
存session: Session["codes"] =要存的session字段 用哈希函数存多个: System.Collections.Hashtable hs = new S ...
- C# 利用反射给不同类型对象同名属性赋值
public class ObjectReflection { public static PropertyInfo[] GetPropertyInfos(Type type) { return ty ...