我有这个简单的代码,按下按钮时运行完美,它会反转我的图像视图的原始图像。这需要3到4秒钟。我想在任务之间添加进度条以通知用户我该怎么做?我在哪里可以添加进度条?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invert_image);
mImageView = (ImageView) findViewById(R.id.myIView);
mImageView.setImageResource(R.mipmap.myimage);
invertButton = (Button) findViewById(R.id.invertButton);
invertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mImageView.buildDrawingCache();
Bitmap image = mImageView.getDrawingCache();
Bitmap readyImage = invertImage(image);
mImageView.setImageBitmap(readyImage);
}
});
}
public Bitmap invertImage(Bitmap original){
Bitmap finalBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), original.getConfig());
int A, R, G, B, pixelColor;
int height = original.getHeight();
int width = original.getWidth();
for(int x = 0; x < height; x++){
for(int y = 0; y < width; y++){
pixelColor = original.getPixel(y,x);
A = Color.alpha(pixelColor);
R = 255 - Color.red(pixelColor);
G = 255 - Color.green(pixelColor);
B = 255 - Color.blue(pixelColor);
finalBitmap.setPixel(y, x, Color.argb(A,R,G,B));
}
}
return finalBitmap;
}
...指导
答案 0 :(得分:1)
你可以这样做,设置进度条,初始for循环的增量为完整循环的一部分,在这种情况下x = height。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define FREQUENCY 5
#define MIN -10
#define MAX 35
int main()
{
time_t t;
/*
* Initialize random number generator
*/
srand((unsigned) time(&t));
/*
* Print random values between -10 and +35°C
*/
while(1)
{
double random = ((double)rand() * (MAX-MIN))/(double)RAND_MAX+MIN;
printf("Temperature = %1.2f @ %d\n", random, system("date"));
fflush(stdout);
sleep(FREQUENCY);
}
}
来自ProgressBar文档。
private ProgressBar mProgress; // Note your will need to implement the progress bar.
public Bitmap invertImage(Bitmap original){
.../...
while (mProgressStatus < 100)
for(int x = 0; x < height; x++){
mProgressStatus = x/height*100;
mProgress.setProgress(mProgressStatus);
for(int y = 0; y < width; y++){
.../...
finalBitmap.setPixel(y, x, Color.argb(A,R,G,B));
}
}
return finalBitmap;
}