Android Matrix postRotate and postTranslate applied to a Bitmap - android

Here is a code snippet which I wrote trying to understand Matrix (postRotate and postTranslate to be more exact).
Activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mImageView2;
#Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.btn:
Matrix matrix = new Matrix();
matrix.postRotate(90);
matrix.postTranslate(200, 0);
Bitmap bitmap =
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, null);
Bitmap bitmap2 = Bitmap.createBitmap(
bitmap,
0,
0,
bitmap.getWidth(),
bitmap.getHeight(),
matrix,
true
);
mImageView2.setImageBitmap(bitmap2);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(this);
mImageView2 = (ImageView) findViewById(R.id.image2);
}
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<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="sample2.com.sample_app.MainActivity">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageView
android:id="#+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
android:scaleType="matrix"
tools:src="#mipmap/ic_launcher" />
</LinearLayout>
The problem is that the bitmap is rotated but not moved anywhere:
What am I doing wrong?

Related

how to set image URL as wallpaper (Glide Library)

please any help , i'm trying to set image as wallpaper i get this image by her url
i'm working with Glide Library i tried this code Set image as wallpaper from url but doesn't work for me please any help for resolve this issue?
file.java :
public class Pop extends Activity {
int width,height;
String url;
LinearLayout llsetwallpapers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop);
llsetwallpapers = (LinearLayout)findViewById(R.id.llSetWallpaper);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
this.width = dm.widthPixels;
this.height = dm.heightPixels;
getWindow().setLayout((int) (((double) this.width) * 0.9d), (int) (((double) this.height) * 0.75d));
getIntent().getSerializableExtra("WallpaperURL");
this.url = (String)getIntent().getSerializableExtra("WallpaperURL");
Glide.with(getApplicationContext()).load(this.url).into((ImageView)findViewById(R.id.imageSelectTo));
llsetwallpapers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// set as wallpapers
}
});
}
}
xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/linealL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#65000000">
<ImageView
android:id="#+id/imageSelectTo"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<!-- Set as wallpaper button -->
<LinearLayout
android:id="#+id/llSetWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:background="#drawable/btn_rounded_corner"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/SetAsWallpaper"
android:textColor="#color/white"
android:textSize="18sp" />
</LinearLayout>
<!-- Download wallpaper button -->
<LinearLayout
android:id="#+id/llDownloadWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:background="#drawable/btn_rounded_corner"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/Download"
android:textColor="#color/white"
android:textSize="18sp" />
</LinearLayout>
private ImageView userImageView;
userImageView = (ImageView) findViewById(R.id.user_image);
Glide.with(getApplicationContext()).load(yourImageUrl).asBitmap()
.error(yourDefaultImageUrl or from R.id.drawableDefaultImage).centerCrop().into(userImageView);
Try this code it take a url converts it into bitmap and then set wallpaper according to screen size :
Declare variables
ImageView mImageView;
WallpaperManager wallpaperManager;
Bitmap bitmap1, bitmap2;
DisplayMetrics displayMetrics; //to find screen dimensions
int width, height; //to find screen dimensions
BitmapDrawable bitmapDrawable;
Button downloadButton;
String url="your wallpaper url";
Initialise your Views
mImageView = (ImageView) findViewById(R.id.imageView);
downloadButton= (Button) findViewById(R.id.Button);
In onCreate
Glide.with(this).load(spacePhoto.getUrl()).asBitmap().listener(new RequestListener<String, Bitmap>() {
#Override
public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
mImageView.setImageBitmap(resource);
//Code to set bitmap as wallpaper according to screen size
fabOptions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Thread(new Runnable() {
#Override
public void run() {
wallpaperManager = WallpaperManager.getInstance(getApplication());
bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();
bitmap1 = bitmapDrawable.getBitmap();
GetScreenWidthHeight();
bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);
wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(bitmap2);
wallpaperManager.suggestDesiredDimensions(width, height);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Snackbar snackbar = Snackbar
.make(v, "Wallpaper Set", Snackbar.LENGTH_SHORT);
snackbar.show();
}
});
return false;
}
}).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mImageView);
Write this code outside of onCreate
public void GetScreenWidthHeight() {
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}

How to re-scale Bitmap image size?

Hi guys I am trying to load a image from a url and after loading that i am trying to re scale it such that it fits the whole screen after that the text and the buttons present are available below the image which are wrapped around using scroll view
this is my fragment
public class FirstFragment extends Fragment {
ImageView im;
Bitmap bitmap;
Drawable dr;
Bitmap bitap;
Bitmap newBitmap;
RelativeLayout rel;
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.first_frag, container, false);
rel = (RelativeLayout) v.findViewById(R.id.relativla);
rel.setVisibility(View.INVISIBLE);
new LoadImage().execute("http://opinions.esy.es/bg.jpg");
Button b = (Button) v.findViewById(R.id.navi);
im = (ImageView) v.findViewById(R.id.imageView);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getContext(), Navi.class);
startActivity(i);
}
});
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
dr = new BitmapDrawable(getResources(),image);
bitap = ((BitmapDrawable) dr).getBitmap();
float scalingFactor = getBitmapScalingFactor(bitap);
Bitmap newBitmap = Util.ScaleBitmap(bitmap, scalingFactor);
im.setImageBitmap(newBitmap);
}else{
Toast.makeText(getContext(), "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
private float getBitmapScalingFactor(Bitmap bm) {
Toast.makeText(getContext(),"entered here",Toast.LENGTH_LONG).show();
// Get display width from device
int displayWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth();
// Get margin to use it for calculating to max width of the ImageView
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)this.im.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
int rightMargin = layoutParams.rightMargin;
// Calculate the max width of the imageView
int imageViewWidth = displayWidth - (leftMargin + rightMargin);
rel.setVisibility(View.VISIBLE);
// Calculate scaling factor and return it
return ( (float) imageViewWidth / (float) bm.getWidth() );
}
}
My Util class
public class Util {
public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
int scaleHeight = (int) (bm.getHeight() * scalingFactor);
int scaleWidth = (int) (bm.getWidth() * scalingFactor);
return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
}
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:fillViewport="true"
>
<RelativeLayout
android:id="#+id/relativla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/hamburger"
android:id="#+id/navi"
android:padding="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:id="#+id/imageView"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diet Plans"
android:padding="10dp"
android:layout_marginTop="10dp"
android:textColor="#android:color/black"
android:textSize="25sp"
android:id="#+id/textView5"
android:layout_below="#+id/imageView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/descrpitiondietplan"
android:textColor="#000000"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textSize="15sp"
android:padding="10dp"
android:id="#+id/textView6"
android:layout_below="#+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Our Diet Plans"
android:textColor="#android:color/black"
android:padding="10dp"
android:textSize="25sp"
android:id="#+id/textView7"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:padding="10dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Basic Diet Plan"
android:textColor="#android:color/white"
android:id="#+id/normmal"
android:background="#color/btn_login"
android:layout_marginBottom="10dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
It the toast insider the getBitmapScaling factor is called after the relative layout is viewed
Hope you guys can help me solve my issue
try add these
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dm.widthPixels, dm.heightPixels);
im.setLayoutParams(params);
below im.setImageBitmap(newBitmap); onPostExecute

Compare 2 ImageViews in android

I currently working on a Drag 'n' Drop application.
My app has 2 layouts.
The 1st layout is 2 balls (Images) of different color.
The 2nd layout is 3 balls (Images) of different color.
When the user drags one ball onto any layout & drops that ball onto the other layout, the app then checks if a ball with the same color is in this layout: if so, it then removes those 2 balls, otherwise adds the ball to that layout.
My XML file
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="#+id/relative">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="150sp"
android:background="#00FF00"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball_red" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball_blue" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball_pink" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/linearLayout1"
android:layout_centerHorizontal="true"
android:background="#0F0F0F">
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ball_red" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ball_yellow" />
</LinearLayout>
</RelativeLayout>
My Activity Class
public class MainActivity extends Activity implements OnTouchListener, OnDragListener{
ImageView i1,i2,i3,i4,i5,i6;
Drawable d1,d2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i1=(ImageView) findViewById(R.id.imageView1); i2=(ImageView) findViewById(R.id.imageView2);
i5=(ImageView) findViewById(R.id.imageView5); i6=(ImageView) findViewById(R.id.imageView6); i4=(ImageView) findViewById(R.id.imageView4);
findViewById(R.id.linearLayout1).setOnDragListener(this);
findViewById(R.id.linearLayout2).setOnDragListener(this);
i6.setOnTouchListener(this); i5.setOnTouchListener(this); i2.setOnTouchListener(this); i1.setOnTouchListener(this);
i4.setOnTouchListener(this);
d1=i1.getDrawable(); d2=i6.getDrawable();
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.VISIBLE);
return true;
}
else {
return false;
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public boolean onDrag(View v, DragEvent e) {
// TODO Auto-generated method stub
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
LinearLayout to = (LinearLayout) v;
boolean comp=false;
comp=compareDrawable(d1, d2);;
if(comp == false)
{
to.addView(view);
view.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Add.", Toast.LENGTH_LONG ).show();
}
else
{
to.removeView(view);
from.addView(view);
view.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Remove.", Toast.LENGTH_LONG ).show();
}
}
return true;
}
public boolean compareDrawable(Drawable d1, Drawable d2){
try{
Bitmap bitmap1 = ((BitmapDrawable)d1).getBitmap();
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
stream1.flush();
byte[] bitmapdata1 = stream1.toByteArray();
stream1.close();
Bitmap bitmap2 = ((BitmapDrawable)d2).getBitmap();
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream2);
stream2.flush();
byte[] bitmapdata2 = stream2.toByteArray();
stream2.close();
return bitmapdata1.equals(bitmapdata2);
}
catch (Exception e) {
// TODO: handle exception
}
return false;
}
}
Get the 2 images ID then compare it,
if(img1.getId()==img2.getId())
{
//Your stuff
}
or try this
Bitmap bitmap = ((BitmapDrawable)img1).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)img2).getBitmap();
if(bitmap == bitmap2)
{
//Code blcok
}
I believe that what you actually need is to compare if two bitmaps are the same content.
For this you must use Bitmap.sameAs(Bitmap) look at it here
As an advice don't compare objects with "==" but using equals!
PS : sameAs method is starting from API 12

Android - multiple draw on ImageView

I'm new in Android and I have a problem with my app: I have a layout with an ImageView and more EditTexts and Buttons. I'm trying to draw a circle on the Image View every time I input coordinates in a EditText and push a button.
I found a way to make it work the first time, but if I just copy the same code for the 2nd button it draws the new circle and erases the first one. And I want to have both of them...
Somebody can tell me whet I do wrong? Thanks a lot.
My activity:
public class IndoorPositioningMainActivity extends Activity {
Button InitialPos = null;
Button P1 = null;
EditText InitialPosX = null;
EditText InitialPosY = null;
EditText InitialPosZ = null;
EditText P1X = null;
EditText P1Y = null;
EditText P1Z = null;
ImageView Image = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_indoor_positioning_main);
InitialPos = (Button)findViewById(R.id.ButtonInitialPosInput);
P1 = (Button)findViewById(R.id.ButtonP1);
InitialPosX = (EditText)findViewById(R.id.InitialPosInputX);
InitialPosY = (EditText)findViewById(R.id.InitialPosInputY);
InitialPosZ = (EditText)findViewById(R.id.InitialPosInputZ);
P1X = (EditText)findViewById(R.id.P1InputX);
P1Y = (EditText)findViewById(R.id.P1InputY);
P1Z = (EditText)findViewById(R.id.P1InputZ);
Image = (ImageView)findViewById(R.id.imageView1);
InitialPos.setOnClickListener(InitialPosListener);
P1.setOnClickListener(P1Listener);
InitialPosX.addTextChangedListener(textWatcher);
InitialPosY.addTextChangedListener(textWatcher);
InitialPosZ.addTextChangedListener(textWatcher);
P1X.addTextChangedListener(textWatcher);
P1Y.addTextChangedListener(textWatcher);
P1Z.addTextChangedListener(textWatcher);
new PositionAsync().execute();
}
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
private OnClickListener InitialPosListener = new OnClickListener() {
#Override
public void onClick(View v) {
String x = InitialPosX.getText().toString();
String z = InitialPosZ.getText().toString();
float x0 = Float.valueOf(x);
float z0 = Float.valueOf(z);
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.est,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(x0, z0, 50, paint);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
}
};
private OnClickListener P1Listener = new OnClickListener() {
#Override
public void onClick(View v) {
String x11 = P1X.getText().toString();
String z11 = P1Z.getText().toString();
float x1 = Float.valueOf(x11);
float z1 = Float.valueOf(z11);
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.est,myOptions);
Paint paint1 = new Paint();
paint1.setAntiAlias(true);
paint1.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(x1, z1, 25, paint1);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
}
};
//XML READER
class PositionAsync extends AsyncTask<Void, Void, Void> {
XMLHelper helper;
#Override
protected Void doInBackground(Void... arg0) {
helper = new XMLHelper();
helper.get();
return null;
}
#Override
protected void onPostExecute(Void result) {
}
}
}
my xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10px" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="centerInside"
android:src="#drawable/est" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="30"
android:orientation="vertical"
android:padding="10px" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/InitialPosX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
<EditText
android:id="#+id/InitialPosInputX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/InitialPosY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y" />
<EditText
android:id="#+id/InitialPosInputY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/InitialPosZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z" />
<EditText
android:id="#+id/InitialPosInputZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<Button
android:id="#+id/ButtonInitialPosInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/P1X"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
<EditText
android:id="#+id/P1InputX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/P1Y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y" />
<EditText
android:id="#+id/P1InputY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/P1Z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z" />
<EditText
android:id="#+id/P1InputZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<Button
android:id="#+id/ButtonP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
</LinearLayout>
Firstly I suppose that "#drawable/est" is the basic image that will be painted. Then Every time that the button is clicked "est" image is reopened so the previous circles aren't saved.
One possible solution is to use a SurfaceView. There are many tutorials on internet about it.
Other solution is not to load "est" image every time, instead of that try saving the canvas and draw the circles one after another.
Hope it helps
The problem is that you only draw 1 circle in your OnClickListener(), and you draw it on a bitmap that you read from a file every time.
Instead you should save the coordinates into a list, and in the click listener draw all the circles from the list.
Like this:
String x11 = P1X.getText().toString();
String z11 = P1Z.getText().toString();
float x1 = Float.valueOf(x11);
float z1 = Float.valueOf(z11);
points.add(new Point(x1, z1));
And then:
for (Point point : points) {
canvas.drawCircle(point.x, point.y, 25, paint1);
}

In Android, when rotate the image it slowly dissappear.

I used the the below code to rotate the image. There is no problem in image rotation, but when i rotate the image the image pixels was reduced. if i rotate continuously means the image will disappeared. How can i solve this problem.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/test" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Click here to Rotate" />
</RelativeLayout>
ImageResizeTestActivity.java
public class ImageResizeTestActivity extends Activity {
/** Called when the activity is first created. */
Button click;
ImageView img;
static File rotated_File;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
click = (Button) findViewById(R.id.button1);
img = (ImageView) findViewById(R.id.imageView1);
String fname = "Rotated_Image.jpg";
rotated_File = new File("/sdcard/" + fname);
click.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (!(rotated_File.exists())) {
Log.v("Inside if", "if");
rotateImage();
} else {
Log.v("Inside else", "else");
rotateImage(rotated_File.getAbsolutePath());
}
}
});
}
protected void rotateImage(String absolutePath) {
// TODO Auto-generated method stub
Bitmap myImg = BitmapFactory.decodeFile(absolutePath);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
myImg.getHeight(), matrix, true);
saveImage_Rotate(rotated);
img.setImageBitmap(rotated);
}
protected void rotateImage() {
// TODO Auto-generated method stub
Bitmap myImg = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
myImg.getHeight(), matrix, true);
saveImage_Rotate(rotated);
img.setImageBitmap(rotated);
}
static void saveImage_Rotate(Bitmap dest2) {
if (rotated_File.exists())
rotated_File.delete();
try {
FileOutputStream out = new FileOutputStream(rotated_File);
dest2.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
after rotating
A quick look on the problem. You save the jpg several times, and during the (lossy)compression it will degrade more and more.
I'd suggest you use png instead

Categories

Resources