/*
Copyright (C) 2015 Haowen Ning
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.liberty.android.fantastischmemo.ui;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Base64;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.simplify.ink.InkView;
import org.liberty.android.fantastischmemo.R;
import org.liberty.android.fantastischmemo.common.BaseActivity;
import java.io.ByteArrayOutputStream;
public class PaintActivity extends BaseActivity {
private InkView inkView;
private Handler handler = new Handler();
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.paint_screen);
setTitle(R.string.paint_text);
inkView = (InkView) findViewById(R.id.ink);
inkView.setColor(Color.WHITE);
inkView.setMinStrokeWidth(1.5f);
inkView.setMaxStrokeWidth(6f);
handler.post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = loadBitmap();
if (bitmap != null) {
inkView.drawBitmap(bitmap, 0f, 0f, null);
}
}
});
}
@Override
public void onDestroy() {
saveBitmap(inkView.getBitmap());
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.paint_screen_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.clear_menu_item:
inkView.clear();
return true;
}
return super.onOptionsItemSelected(item);
}
private Bitmap loadBitmap() {
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
String previouslyEncodedImage = shre.getString("paint_image_data", "");
if( !previouslyEncodedImage.equalsIgnoreCase("") ){
byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
return mutableBitmap;
}
Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int width = display.getWidth();
@SuppressWarnings("deprecation")
int height = display.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
return bitmap;
}
private void saveBitmap(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit=shre.edit();
edit.putString("paint_image_data",encodedImage);
edit.commit();
}
}