今天学习了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记步数(画圆形图片知识)的更多相关文章

  1. 在android中画圆形图片的几种办法

    在开发中常常会有一些需求,比方显示头像,显示一些特殊的需求,将图片显示成圆角或者圆形或者其它的一些形状. 可是往往我们手上的图片或者从server获取到的图片都是方形的.这时候就须要我们自己进行处理, ...

  2. android绘制圆形图片的两种方式

    看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...

  3. iOS常见用户头像的圆形图片裁剪常见的几种方法

    在开发中,基本上APP的用户头像的处理都需要把用户所上传的方形图片,处理为圆形图片.在这里就总结三种常见的处理圆形图片的方法. 1.使用位图上下文 2.使用UIView的layer进行处理 3.使用r ...

  4. Android实现圆形图片

     情景再现: 写Android程序也有一段时间了,今天突然被问怎么实现一个圆形图片,很多app图像是圆形的.但是用户上传的图像可不是圆的,所以问题就来了,需要我们代码实现圆形图片.但是大脑飞转想到第三 ...

  5. UIImage类扩展返回一个带边框的圆形图片

    /** * 将image转换为圆型带边框的图片(最好写一个UIImage的类扩展) * * @param name 图片的名字 * @param borderWidth 外层边框的宽度 * @para ...

  6. Android圆形图片--ImageView

    [ RoundImageView.java ] package com.dxd.roundimageview; import android.content.Context; import andro ...

  7. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

  8. 使用CAShapeLayer来实现圆形图片加载动画[译]

    原文链接 : How To Implement A Circular Image Loader Animation with CAShapeLayer 原文作者 : Rounak Jain 译文出自 ...

  9. Android圆形图片自定义控件

    Android圆形图片控件效果图如下: 代码如下: RoundImageView.java package com.dxd.roundimageview; import android.content ...

随机推荐

  1. 【node】使用gulp来维护网站项目

    基本参照此:http://www.gulpjs.com.cn/docs/getting-started/ 1.电脑需要安装好nodejs,安装好的时候会自带npm 2.在命令行中执行命令安装gulp ...

  2. I prefer fcitx

    sudo add-apt-repository ppa:fcitx-team/nightly sudo apt-get update Then, you should install Fcitx.Fc ...

  3. ASP.NET 服务器控件的生命周期

    服务器控件生命周期简介 服务器控件的生命周期是创建服务器控件最重要的概念.作为开发人员,必须对服务器控件生命周期深刻理解.当然,这不是一朝一夕就可以做到的.对于学习控件开发技术的初学者,可以不必掌握得 ...

  4. SQL Server获取月度列表

    -- 获取月度列表 if exists(select 1 from sysobjects where name = 'proc_GetDateMonthList' and type = 'p') dr ...

  5. C++设计模式-Memento备忘录模式

    Memento模式作用:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态. UML图: Originator:负责创建一个备忘录Me ...

  6. storm 配置,呵呵。

    配置项 配置说明 storm.zookeeper.servers ZooKeeper服务器列表 storm.zookeeper.port ZooKeeper连接端口 storm.local.dir s ...

  7. extentreports报告插件与testng集成(二)

    之前的一篇文章中,是把extentreports 的报告的初始方法写在driver的初始方法中,写报告的方法在testng的 onTest中,这次将这些方法全都拆出来,写在一个方法类中,这个类重现实现 ...

  8. 使用Asp.net WebAPI 快速构建后台数据接口

    现在的互联网应用,无论是web应用,还是移动APP,基本都需要实现非常多的数据访问接口.其实对一些轻应用来说Asp.net WebAPI是一个很快捷简单并且易于维护的后台数据接口框架.下面我们来快速构 ...

  9. Eclipse使用tomcat的原理

    1. 当我们使用Eclipse将项目部署到Tomcat的时,我们发现,在Tomcat下的webapps目录下并没有我们创建好的项目,但是当通过Eclipse启动服务器后,项目却真的可以访问到,这是为什 ...

  10. PSP个人(观众界面)

    需求:作为一个观众,我希望了解某一场比赛的比分,以便了解赛况.(满意条件:精确到每一局的结果比分) 需求分析:实现查询数据库中每一局的分数并用界面显示. 生成设计文档: 运用三层架构,实现软件的基本功 ...