How to faster renew ImageView by Bitmap - android

I want to renew imageView fastly. My code is that ImageView is changed every 3 colors. It operates correctly. but very slow. I want to faster operate the code. what should i do? This is my code.
public class MainActivity extends AppCompatActivity {
ImageView img;
int[] arr = new int[300];
int colorArr[] = {Color.BLACK, Color.GREEN, Color.RED};
char color = 0;
DisplayMetrics dm;
Bitmap bit;
int x = 1, y = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.image);
bit = Bitmap.createBitmap(300, 400, RGB_565);
for(int i = 0 ; i < 300; i++)
arr[i] = colorArr[color%3];
for(int i = 0; i < 300; i++){
for(int j = 0; j < 400; j++)
bit.setPixel(i,j, Color.WHITE);
}
img.post(new Runnable() {
#Override
public void run() {
bit.setPixels(arr, 0, 300, 0, y++, 300,1);
img.setImageBitmap(bit);
img.post(this);
if(y == 400){
color++;
for(int i = 0; i < 300; i++)
arr[i] = colorArr[color%3];
y = 0;
}
}
});
}
}

Related

Can not draw a shape in android, getting "ioctl c0044901 failed with code -1: Invalid argument" error?

I am trying to draw circle in an android app which I am building using android studio.
Every time I run the code on my connected device, it shows a blank screen with no shape on it. It is a circle I am trying to draw. Actually a pie chart at the end. Neglect the constructor and setParts() method.
I am sharing the code. Please guide me regarding this problem.
public class MainActivity extends AppCompatActivity {
PieChartView pieChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new PieChartView(this));
}
public class PieChartView extends android.view.View{
int totalNumberOfParts,unitPart,sections;
int arrayLength;
int[] arrayOfNumbers;
public PieChartView(Context context){
super(context);
arrayOfNumbers = new int[10];
arrayLength = 7;
totalNumberOfParts = 0;
unitPart = 0;
totalNumberOfParts = 0;
sections = 7;
arrayOfNumbers[0] = 20;
arrayOfNumbers[1] = 30;
arrayOfNumbers[2] = 10;
arrayOfNumbers[3] = 9;
arrayOfNumbers[4] = 12;
arrayOfNumbers[5] = 16;
arrayOfNumbers[6] = 21;
}
public void setParts(int total,int numberOfSections){
this.totalNumberOfParts = total;
this.unitPart = 360/total;
this.sections = numberOfSections;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println("Entered onDraw method");
Paint paint = new Paint();
canvas = new Canvas();
canvas.drawPaint(paint);
int color = Color.parseColor("red");
paint.setColor(color);
canvas.drawCircle(5,5,10,paint);
/* float initialAngle = 0f;
int i=0;
int j=0;
int k =1;
int x =0;
int con = 90;
int red = 7;
int green =0;
int blue= 0;
int colorindex = 0;
String[] hexStringColorArray = {"#808000","#808080","#800000","#C0C0C0","#000000","#FFFFFF","#FF0000","#00FF00","#0000FF","#FFFF00","#00FFFF","#FF00FF"};
while(i<sections){
System.out.println("entered");
RectF rect= new RectF(50f, 50f, 100f,100f);
canvas.drawArc(rect,initialAngle,(unitPart*arrayOfNumbers[i]),true,paint);
initialAngle+=unitPart*arrayOfNumbers[i];
i++;
System.out.println("loop complete");
//System.out.println("i = "+i);
//System.out.println("j = "+con*j);
// System.out.println("k = "+con*k);
//System.out.println("hexStringColorArray[colorindex] "+hexStringColorArray[colorindex]);
//colorindex+=1;
}*/
}
}
}

RecyclerViews and AnimationDrawables - how can I put AnimationDrawables into a ViewHolder without them stopping on having another view in the window?

Basically, I have an AnimationDrawable that I generated with a helper method:
public static AnimationDrawable requestLoadingDrawable(Context originContext) {
if(panoramaLoadingLoopDrawable == null) {
panoramaLoadingLoopDrawable = new AnimationDrawable();
Bitmap src = BitmapFactory.decodeResource(originContext.getResources(), R.drawable.load360_3lines);
BitmapDrawable frameInDrawable;
//int squareInstDim = (int)(200*adapterCtx.getResources().getDisplayMetrics().density);
int horiDim = (int)(src.getWidth() / 10);
int vertDim = (int)(src.getHeight() / 3);
Bitmap frame;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 10; j++) {
frame = Bitmap.createBitmap(src, j * horiDim, i*vertDim, horiDim, vertDim);
frameInDrawable = new BitmapDrawable(originContext.getResources(), frame);
panoramaLoadingLoopDrawable.addFrame(frameInDrawable, (1000/30));
}
}
src.recycle();
}
panoramaLoadingLoopDrawable.setOneShot(false);
return panoramaLoadingLoopDrawable;
}
The panoramaLoadingLoopDrawable is private static in this helper class.
When I put it in the RecyclerView, when there's 2 items that use this AnimationDrawable in the ViewHolder, both would stop animating.
Should I make the panoramaLoadingLoopDrawable non-static or something?

android ImageView is not Working

I want to renew ImageView whenever set pixel of Bitmap class. but ImageView is not working. I already use ui thread. what should i do?
this is my code.
public class MainActivity extends AppCompatActivity {
ImageView img;
DisplayMetrics dm;
Bitmap bit;
int x = 1, y = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.image);
bit = Bitmap.createBitmap(300, 400, RGB_565);
for(int i = 0; i < 300; i++){
for(int j = 0; j < 400; j++)
bit.setPixel(i,j, Color.WHITE);
}
runOnUiThread(new Runnable() {
#Override
public void run() {
while(true) {
bit.setPixel(x++, y++, Color.BLACK);
img.setImageBitmap(bit);
img.invalidate();
try{
Thread.sleep(100);
}catch (Exception e){
}
}
}
});
}
}
Use postDelayed into your ImageView.post() method. Here I have done this on my machine. Plus don't use while(true) stop it when your pixels ends.
img = (ImageView) findViewById(R.id.image);
bit = Bitmap.createBitmap(300, 400, RGB_565);
for(int i = 0; i < 300; i++){
for(int j = 0; j < 400; j++)
bit.setPixel(i,j, Color.WHITE);
}
img.setImageBitmap(bit);
img.post(new Runnable() {
#Override
public void run() {
bit.setPixel(x, y, Color.BLACK);
img.setImageBitmap(bit);
if(++x < 300 && ++y < 400){
img.postDelayed(this, 100);
}
}
});
Try to update View using Handler.
img.post(new Runnable() {
#Override
public void run() {
}
});

Error in Android application AsyncTask

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
I am new to Android and Googled a lot but not getting the context. I am performing matrix multiplication of very large number using AsyncTask but getting error. Any help will be appreciated.
Error:(77, 13) error: method does not override or implement a method from a supertype
add the onPostExecute inside the AsyncTask Class.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
}
you can add the method onPostExecute() inside the AsyncTask.

How to scale the images so that they should occupy the entire screen

I am developing a brick game here is the code
public class Sprite {
private static final int NO_COLUMNS = 12;
Bitmap bmp,bar,brick,scalebit;
GameView gameview;
int x,speedx=0,speedy=0;
int no_bricksperline;
private int currentFrame=0;
private int width;
private int height;
int brwidth;
int brheight;
int init=0;
float tempwidth;
String[][] bricks;
GameThread thread;
private int y;
Paint paint=new Paint();
private int barx;
private int bary;
#SuppressLint("FloatMath")
public Sprite(GameView gameview,GameThread thread,Bitmap bmp,Bitmap bar,Bitmap brick)
{
this.gameview=gameview;
this.bmp=bmp;
this.bar=bar;
this.brick=brick;
this.thread=thread;
this.no_bricksperline=gameview.getWidth()/brick.getWidth()-2;
bricks=new String[no_bricksperline][4];
brwidth=brick.getWidth();
brheight=brick.getHeight();
paint.setColor(Color.BLACK);
for (int i = 0; i < no_bricksperline; ++i)
for (int j = 0; j < 4; ++j)
bricks[i][j] = "B";
String strwidth=String.valueOf(((float)(bmp.getWidth())/NO_COLUMNS));
if(strwidth.contains("."))
{
scalebit=Bitmap.createScaledBitmap(bmp, (int)(Math.ceil(((float)bmp.getWidth())/NO_COLUMNS))*NO_COLUMNS, bmp.getHeight(), true);
}
else
{
scalebit=bmp;
}
this.width=scalebit.getWidth()/NO_COLUMNS;
this.bary=gameview.getHeight()-100;
this.height=scalebit.getHeight();
Random random=new Random();
x = random.nextInt(gameview.getWidth() - width);
y = random.nextInt(gameview.getHeight() - height)-100;
if(y<=100)
{
y=120;
}
speedx=random.nextInt(10)-5;
speedy=random.nextInt(10)-5;
}
public void update()
{
if(x>gameview.getWidth()-width-speedx || x+speedx<0)
{
speedx=-speedx;
}
if(y>gameview.getHeight()-height-speedy || y+speedy<0)
{
speedy=-speedy;
}
if(y+height >= bary-bar.getHeight()) {
if((x+width>barx-bar.getWidth()/2 && barx>x) || (x<barx+bar.getWidth()/2 && x>barx)) {
speedy=-speedy;
}
}
if(y>0 && y<=(5*brheight) && init==1)
{
if(x!=0){
int x1=x;
int y1=y;
if(x1>(no_bricksperline+1)*brwidth)
{
x1=(no_bricksperline)*brwidth;
}
if(x1<=brwidth)
{
x1=brwidth;
}
if(y1<=brheight)
{
y1=brheight;
}
if(y1>=(4*brheight) && y1<=(5*brheight))
{
y1=4*brheight;
}
int posi=(int)Math.floor(x1/brwidth)-1;
int posj=(int)Math.floor(y1/brheight)-1;
if(bricks[posi][posj]=="B")
{
bricks[posi][posj]="K";
speedy=-speedy;
}
}
}
if(y+height>bary+bar.getHeight())
{
}
x=x+speedx;
y=y+speedy;
currentFrame=++currentFrame%NO_COLUMNS;
}
#SuppressLint("DrawAllocation")
public void onDraw(Canvas canvas)
{
checkFinish();
update();
for (int i = 0; i < no_bricksperline; ++i){
for (int j = 0; j < 4; ++j) {
// ourHolder.lockCanvas();
if (bricks[i][j].contains("B"))
canvas.drawBitmap(brick, (i+1)*brick.getWidth(),(j+1)*brick.getHeight(),
null);
init=1;
}
}
int srcX=currentFrame*width;
int srcY=0;
Rect src=new Rect(srcX, srcY, srcX+width, srcY+height);
Rect dest=new Rect(x,y,x+width,y+width);
canvas.drawBitmap(scalebit,src,dest,null);
canvas.drawBitmap(bar, barx-bar.getWidth()/2, bary-bar.getHeight(),null);
}
private void checkFinish() {
int totalbricks=0;
for (int i = 0; i < no_bricksperline; ++i){
for (int j = 0; j < 4; ++j){
if(bricks[i][j] == "K"){
totalbricks++;
}
}
}
if(totalbricks==(no_bricksperline*4))
{
thread.setRunning(false);}
}
public void onTouch(MotionEvent event) {
barx= (int) event.getX();
}
}
the problem is bricks are displaying
When I use the brick image directly their are pixlated and also only 2 bricks are displaying. So I have write a logic to display min no of bricks
I have used some logic in my code
here is the code
String strwidth=String.valueOf(((float)(bmp.getWidth())/NO_COLUMNS));
if(strwidth.contains("."))
{
scalebit=Bitmap.createScaledBitmap(bmp, (int)(Math.ceil(((float)bmp.getWidth())/NO_COLUMNS))*NO_COLUMNS, bmp.getHeight(), true);
}
else
{
scalebit=bmp;
}
but there is a gap between walls and bricks. How can I make the bricks to occupy the entire screen.
use this
this.no_bricksperline=gameview.getWidth()/brick.getWidth();
instead of
this.no_bricksperline=gameview.getWidth()/brick.getWidth()-2;
If i have understood you correctly, your problem is the width of the bricks to be adjusted to fill the entire screen.
try to use this one
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
dispWidth=metrics.widthPixels;
//make the dispWidth your gameView width
use the imageview property
android:scaleType="..."

Categories

Resources