//学了个Android每年都要做个实例真是辛苦,现在贴个代码放这里看看
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_sky"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/ly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:background="@drawable/tv_password" />
<EditText
android:id="@+id/ed_password"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:gravity="top|left"
android:ems="10"
android:inputType="textPassword"
android:background="@drawable/edv_line" />
</LinearLayout>
<com.wl.android.myeditView.MyImageView
android:id="@+id/img_user"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_above="@id/ly"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:src="@color/grayPuples" />
<com.wl.android.myeditView.MyImageView
android:id="@+id/anmitor"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_above="@id/ly"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:src="@drawable/img_user_login" />
<TextView
android:id="@+id/tv_frogivelogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:text="忘记密码?"
android:textColor="@drawable/move_btn"
android:textSize="20dp" />
</RelativeLayout>
</LinearLayout>
4.1.2 主界面XML
<pre name="code" class="html"><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:background="@drawable/bg_green_02"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/grayGreen_alph70" >
<Button
android:id="@+id/btn_menu"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@color/nocolor"
android:text="菜单"
android:textColor="@drawable/move_btn_menu"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:text="TimelineNote"
android:textColor="@color/white"
android:textSize="25dp" />
<Button
android:id="@+id/btn_add"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/nocolor"
android:text="新增"
android:textColor="@drawable/move_btn_menu"
android:textSize="20dp" />
</RelativeLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="0dp" />
</LinearLayout>
4.1.3 密码界面XML
<?xml version="1.0" encoding="utf-8"?>
//顶部栏
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_flower"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="top"
android:background="@color/blue_light" >
<Button
android:id="@+id/btn_resist_cancle"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@color/nocolor"
android:text="返回"
android:textColor="@color/blue_nature"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:text="设置密码"
android:textColor="@color/blue_nature"
android:textSize="20dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
//设置密码时显示的内容
<LinearLayout
android:id="@+id/ly_set_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:layout_width="230dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="密码"
android:textColor="@color/blue_nature"
android:textSize="20dp" />
<EditText
android:id="@+id/resist_password"
android:layout_width="230dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@color/blue_light" />
<TextView
android:layout_width="230dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="密保问题"
android:textColor="@color/blue_nature"
android:textSize="20dp" />
<EditText
android:id="@+id/resist_question"
android:layout_width="230dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@color/blue_light" />
<TextView
android:layout_width="230dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="答案"
android:textColor="@color/blue_nature"
android:textSize="20dp" />
<EditText
android:id="@+id/resist_answer"
android:layout_width="230dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@color/blue_light" />
</LinearLayout>
//修改密码 和 删除密码时显示的内容
<LinearLayout
android:id="@+id/ly_ensure_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:layout_width="230dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="请输入密码"
android:textColor="@color/blue_nature"
android:textSize="20dp" />
<EditText
android:id="@+id/edt_ensure_password"
android:layout_width="230dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:password="true"
android:background="@color/blue_light" />
<TextView
android:id="@+id/tv_forgive"
android:layout_width="230dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="忘记密码?"
android:textColor="@drawable/move_btn"
android:textSize="20dp" />
</LinearLayout>
//忘记密码是显示的内容
<LinearLayout
android:id="@+id/ly_answerquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_answer_question"
android:layout_width="230dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="what is your name?"
android:textColor="@color/blue_nature"
android:textSize="20dp" />
<EditText
android:id="@+id/ed_answer_question"
android:layout_width="230dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@color/blue_light" />
</LinearLayout>
<Button
android:id="@+id/btn_ensure"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:text="下一步"
android:gravity="center"
android:textColor="@color/blue_nature"
android:background="@color/blue_light" />
</LinearLayout>
</LinearLayout>
4.1.4 编辑界面XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_plante"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/grayBlown_alph70" >
<Button
android:id="@+id/back"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="@color/nocolor"
android:text="取消"
android:textColor="@drawable/cancle_btn_x"
android:textSize="20dp" />
<TextView
android:id="@+id/showdate"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="2016/5/23"
android:textColor="@color/white"
android:textSize="25dp" />
<Button
android:id="@+id/ok"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/nocolor"
android:text="保存"
android:textColor="@drawable/cancle_btn_x"
android:textSize="20dp" />
</RelativeLayout>
//文本编辑区
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:orientation="vertical" >
<com.wl.android.myeditView.MyEditView
android:id="@+id/edittitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:padding="10dp"
android:singleLine="true"
android:textColor="@color/grayBlown_press"
android:textSize="30dp" />
<com.wl.android.myeditView.MyEditView
android:id="@+id/editcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:background="@color/nocolor"
android:ems="10"
android:gravity="top"
android:padding="10dp"
android:textColor="@color/grayBlown_press"
android:textSize="30dp" >
<requestFocus />
</com.wl.android.myeditView.MyEditView>
</LinearLayout>
//图片增加区
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
<Button
android:id="@+id/addpicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="3dp"
android:background="@drawable/img_view_top"
android:gravity="center" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/imagewindow"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/img_view"
android:orientation="vertical"
android:visibility="gone" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView0"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/img_add_x" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/img_add_x" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/img_add_x" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="3dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/img_add_x" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
4.2.1 LoginActivity
package com.example.timelinenote;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.wl.android.DateSQLite.DateSQlite;
import com.wl.android.animation.MyAnimation;
public class LoginActivity extends Activity implements OnClickListener{
//判断输入密码的正确与误时进行的操作
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO 自动生成的方法存根
super.handleMessage(msg);
if (1 == msg.what) {
Toast.makeText(LoginActivity.this, "登录成功!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
LoginActivity.this.startActivity(intent);
LoginActivity.this.finish();
}else if (2 == msg.what) {
Toast.makeText(LoginActivity.this, "请重新登录!",
Toast.LENGTH_SHORT).show();
miv_user.startAnimation(mAnimation.ButtonClickClose());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO 自动生成的方法存根
handler.sendEmptyMessage(3);
}
}, 1000);
}else if (3 == msg.what) {
miv_user.startAnimation(mAnimation.setAnimationSet());
}
}
};
DateSQlite mSQL;
String mstrPassword;
ImageView miv_user;
MyAnimation mAnimation;
TextView mtv_forget;
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mSQL = new DateSQlite(this);
//获得数据库中的密码
try {
Cursor cursor = mSQL.getInformation();
cursor.moveToFirst();
mstrPassword = cursor.getString(mSQL.KEY);
System.out.println(mstrPassword);
cursor.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("无密码或密码获取错误");
mstrPassword = "0";
}
mAnimation = new MyAnimation();
miv_user = (ImageView) findViewById(R.id.img_user);
mtv_forget = (TextView) findViewById(R.id.tv_frogivelogin);
mtv_forget.setOnClickListener(this);
miv_user.setOnClickListener(this);
//给头像增加动画
miv_user.setAnimation(mAnimation.setAnimationSet());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_user:
miv_user.startAnimation(mAnimation.ButtonClickOpen());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO 自动生成的方法存根
logIn();
}
}, 1500);
break;
case R.id.tv_frogivelogin:
Intent intent = new Intent(LoginActivity.this, ResistitActivity.class);
intent.putExtra("type", "4");
LoginActivity.this.startActivity(intent);
default:
break;
}
}
//判断密码输入是否正确
public void logIn() {
new Thread(){
public void run() {
String strPassword = ((EditText)findViewById(R.id.ed_password)).getText().toString();
if ((!mstrPassword.equals("0")) && strPassword.equals(mstrPassword)) {//有密码的情况且正确
handler.sendEmptyMessage(1);
}else if (mstrPassword.equals("0")) {//没有密码的情况
handler.sendEmptyMessage(1);
}else {
handler.sendEmptyMessage(2);//有密码但错误
}
};
}.start();
}
}
4.2.2 IndexActivity
package com.example.timelinenote;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import com.wl.android.DateSQLite.DateSQlite;
import com.wl.android.enpty.Logs;
import com.wl.android.myadapter.Myadapter;
public class IndexActivity extends Activity implements OnClickListener,OnItemClickListener,OnItemLongClickListener{
private ListView mlistview;
ArrayList<Logs> mlistLogs = new ArrayList<Logs>();
final int HEADER_CODE = 1;
DateSQlite mSQL;
android.widget.PopupMenu mpopupMenu = null;
Button mbtnMenu;
Typeface mTypeface_HM;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
mTypeface_HM = Typeface.createFromAsset(getAssets(), "fonts/Hym.ttf");//设置字体
mlistview = (ListView) findViewById(R.id.listview);
View ListViewHeader = View.inflate(this, R.layout.listview_header, null);
mlistview.addHeaderView(ListViewHeader);
Button addbButton = (Button) findViewById(R.id.btn_add);
mbtnMenu = (Button) findViewById(R.id.btn_menu);
mSQL = new DateSQlite(this);
initDatas();
addbButton.setOnClickListener(this);
mlistview.setOnItemClickListener(this);
mlistview.setOnItemLongClickListener(this);
mbtnMenu.setOnClickListener(this);
try {//从数据库中获得内容
mlistLogs = mSQL.getLogs();
initDatas();
} catch (Exception e) {
// TODO: handle exception
}
}
//往listView中填充内容
private void initDatas() {
// TODO 自动生成的方法存根
Myadapter myadapter = new Myadapter(mlistLogs, this);
mlistview.setAdapter(myadapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO 自动生成的方法存根
super.onActivityResult(requestCode, resultCode, data);
//对EditView中传递的内容进行操作
if (0 == resultCode) {
Bundle bundle = data.getExtras();
Logs dateLogs = (Logs) (bundle.get("Logs"));
Boolean isedit = data.getExtras().getBoolean("isedit");
int position = data.getExtras().getInt("position");
if (isedit) {
mlistLogs.remove(position - HEADER_CODE);
mlistLogs.add(position - HEADER_CODE,dateLogs);
}else {
//listview 增加 header 之后产生的1个位子的位移
mlistLogs.add(0,dateLogs);
}
}
initDatas();
}
@Override
protected void onPause() {
// TODO 自动生成的方法存根
super.onPause();
mSQL.insertLogs(mlistLogs);
}
//点击listViewItem时的反应
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO 自动生成的方法存根
if (0 != position) {
Intent intent = new Intent(IndexActivity.this, EditActivity.class);
intent.putExtra("isadd", false);//通知EditActivity不是增加内容
intent.putExtra("position", position);
intent.putExtra("Logs", mlistLogs.get(position - HEADER_CODE));//这个减去Header_code是为了防止点击listView的Header
startActivityForResult(intent, 0);
}
}
//顶部栏按钮的监听事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
Intent intent = new Intent();
intent.setClass(IndexActivity.this, EditActivity.class);
IndexActivity.this.startActivityForResult(intent, 0);
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);//Activity跳转的动画
break;
case R.id.btn_menu:
PopupMenu(mbtnMenu);
default:
break;
}
}
//长按删除日志
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog.Builder deleteBuilder = new AlertDialog.Builder(this);
deleteBuilder.setMessage("你确定删除这篇日记吗?");
final int KEY = position - HEADER_CODE;
deleteBuilder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mSQL.deleteLog(mlistLogs.get(KEY));
mlistLogs.remove(KEY);
initDatas();
}
});
deleteBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
}
});
deleteBuilder.create();
deleteBuilder.show();
return true;
}
//设置密码的菜单
public void PopupMenu(View button)
{
mpopupMenu = new PopupMenu(this, button);
getMenuInflater().inflate(R.menu.menu, mpopupMenu.getMenu());
mpopupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(IndexActivity.this, ResistitActivity.class);
switch (item.getItemId()) {
case R.id.menu_im_setPassword:
intent.putExtra("type", "1");
IndexActivity.this.startActivity(intent);
break;
case R.id.menu_im_alterPassword:
intent.putExtra("type", "2");
IndexActivity.this.startActivity(intent);
break;
case R.id.menu_im_deletePassword:
intent.putExtra("type", "3");
IndexActivity.this.startActivity(intent);
break;
default:
break;
}
return false;
}
});
mpopupMenu.show();
}
}
4.2.3 ResistActivity
package com.example.timelinenote;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.wl.android.DateSQLite.DateSQlite;
public class ResistitActivity extends Activity implements OnClickListener {
DateSQlite db;
EditText resistPassword;
EditText resisqustion;
EditText resistanswer;
LinearLayout ly_ensure_mPassword;
LinearLayout ly_set_mPassword;
LinearLayout ly_answer_mQuestion;
private String mPassword;
private String mQuestion;
private String mKey;
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.resist);
ly_ensure_mPassword = (LinearLayout) findViewById(R.id.ly_ensure_password);
ly_answer_mQuestion = (LinearLayout) findViewById(R.id.ly_answerquestion);
ly_set_mPassword = (LinearLayout) findViewById(R.id.ly_set_password);
resistPassword = (EditText) findViewById(R.id.resist_password);
resisqustion = (EditText) findViewById(R.id.resist_question);
resistanswer = (EditText) findViewById(R.id.resist_answer);
findViewById(R.id.btn_resist_cancle).setOnClickListener(this);
db = new DateSQlite(this);
Intent intent = getIntent();
String typeString = intent.getStringExtra("type");
try {
Cursor cursor = db.getInformation();
cursor.moveToFirst();
mPassword = cursor.getString(db.PASSWORD);
mQuestion = cursor.getString(db.QUESTION);
mKey = cursor.getString(db.KEY);
} catch (Exception e) {
// TODO: handle exception
mPassword = "0";
mQuestion = "0";
mKey = "0";
}
if (typeString.equals("1")) {
if (mPassword.equals("0")) {
setPassword();
}else {
Toast.makeText(ResistitActivity.this, "密码已经设置了哦!", Toast.LENGTH_LONG).show();
}
} else if (typeString.equals("2")) {
alterPassword();
} else if (typeString.equals("3")) {
deletePassword();
}else {
FogivePassword();
}
}
private void deletePassword() {
// TODO 自动生成的方法存根
ly_ensure_mPassword.setVisibility(View.VISIBLE);
TextView tv_answer = (TextView) findViewById(R.id.tv_forgive);
tv_answer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
FogivePassword();
ly_ensure_mPassword.setVisibility(View.GONE);
return;
}
});
Button btn_ensure = (Button) findViewById(R.id.btn_ensure);
btn_ensure.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
EditText edt_ensur = (EditText) findViewById(R.id.edt_ensure_password);
final String ed_mPassword = edt_ensur.getText().toString();
if (ed_mPassword.equals(mPassword)) {
db.deleteUser();
Toast.makeText(ResistitActivity.this, "密码删除成功", Toast.LENGTH_LONG).show();
findViewById(R.id.ly_ensure_password).setVisibility(View.GONE);
}else {
Toast.makeText(ResistitActivity.this, "密码错误", Toast.LENGTH_LONG).show();
}
}
});
}
private void alterPassword() {
// TODO 自动生成的方法存根
ly_ensure_mPassword.setVisibility(View.VISIBLE);
TextView tv_answer = (TextView) findViewById(R.id.tv_forgive);
tv_answer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
FogivePassword();
ly_ensure_mPassword.setVisibility(View.GONE);
return;
}
});
Button btn_ensure = (Button) findViewById(R.id.btn_ensure);
btn_ensure.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
EditText edt_ensur = (EditText) findViewById(R.id.edt_ensure_password);
String ed_mPassword = edt_ensur.getText().toString();
if (ed_mPassword.equals(mPassword)) {
setPassword();
ly_ensure_mPassword.setVisibility(View.GONE);
}else {
Toast.makeText(ResistitActivity.this, "密码错误", Toast.LENGTH_LONG).show();
}
}
});
}
private void setPassword() {
ly_set_mPassword.setVisibility(View.VISIBLE);
Button btn_ensure = (Button) findViewById(R.id.btn_ensure);
btn_ensure.setText("确定");
btn_ensure.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
risistmPassword();
Toast.makeText(ResistitActivity.this, "密码设置成功", Toast.LENGTH_LONG).show();
ResistitActivity.this.finish();
}
});
}
private void FogivePassword()
{
ly_answer_mQuestion.setVisibility(View.VISIBLE);
TextView tv_qusetion = (TextView) findViewById(R.id.tv_answer_question);
tv_qusetion.setText(mQuestion);
Button btn_ensure = (Button) findViewById(R.id.btn_ensure);
btn_ensure.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
EditText ed_answer = (EditText) findViewById(R.id.ed_answer_question);
String answersString = ed_answer.getText().toString();
if (answersString.equals(mKey)) {
Toast.makeText(ResistitActivity.this, "恭喜回答正确", Toast.LENGTH_LONG).show();
ly_answer_mQuestion.setVisibility(View.GONE);
setPassword();
}else {
Toast.makeText(ResistitActivity.this, "回答错误呢!", Toast.LENGTH_LONG).show();
}
}
});
}
private Boolean getInf() {
try {
Cursor cursor = db.getInformation();
cursor.moveToFirst();
mPassword = cursor.getString(db.KEY);
mQuestion = cursor.getString(db.QUESTION);
mKey = cursor.getString(db.KEY);
cursor.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("无密码或密码获取错误");
return false;
}
return true;
}
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.btn_resist_cancle:
this.finish();
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO 自动生成的方法存根
super.onActivityResult(requestCode, resultCode, data);
}
private boolean risistmPassword() {
// TODO 自动生成的方法存根
String mPassword = resistPassword.getText().toString();
String mQuestion = resisqustion.getText().toString();
String mKey = resistanswer.getText().toString();
db.insertInformation(mPassword, mQuestion, mKey);
return true;
}
}
4.2.4 EditActivity
package com.example.timelinenote;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.style.TypefaceSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.wl.android.DateSQLite.DateSQlite;
import com.wl.android.enpty.GetPictureFormCAMER;
import com.wl.android.enpty.Logs;
import com.wl.android.myeditView.MyEditView;
public class EditActivity extends Activity implements OnClickListener,
OnTouchListener, OnLongClickListener {
Logs mLogs;
MyEditView medt_title;
MyEditView medt_content;
Button mbtn_back;
Button mbtn_right;
Button mbtn_addPicture;
String mstrTitle;
String mstrContent;
String mstrDate;
TextView mtv_showDate;
String[] mstrImgList = { "0", "0", "0", "0" };
RelativeLayout mRL_imgGroup;
GetPictureFormCAMER mGetPicture;
int[] mintImgId = { R.id.imageView0, R.id.imageView1, R.id.imageView2,
R.id.imageView3 };
int mintFlat = 0;
int mintImgFlat = 0;
final String BTN_SAVE = "save";
final String BTN_ALTER = "alter";
final int CAMERA = 120;
int position;
Boolean isadd = true;
Typeface mTypeface_HM;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View parentView = View.inflate(getBaseContext(), R.layout.editview,
null);
setContentView(parentView);
mTypeface_HM = Typeface.createFromAsset(getAssets(), "fonts/Hym.ttf");
mLogs = new Logs();
medt_title = (MyEditView) findViewById(R.id.edittitle);
mtv_showDate = (TextView) findViewById(R.id.showdate);
medt_content = (MyEditView) findViewById(R.id.editcontent);
medt_title.setTypeface(mTypeface_HM);
medt_content.setTypeface(mTypeface_HM);
mtv_showDate.setTypeface(mTypeface_HM);
mbtn_back = (Button) findViewById(R.id.back);
mbtn_right = (Button) findViewById(R.id.ok);
mbtn_right.setTag(BTN_SAVE);
mbtn_addPicture = (Button) findViewById(R.id.addpicture);
mRL_imgGroup = (RelativeLayout) findViewById(R.id.imagewindow);
medt_content.setOnTouchListener(this);
mGetPicture = new GetPictureFormCAMER(this);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy/hhmmss");
Date date = new Date(System.currentTimeMillis());
mstrDate = format.format(date);
mtv_showDate.setText(mstrDate);
if (null != getIntent().getExtras()) {
mLogs = (Logs) getIntent().getExtras().get("Logs");
isadd = getIntent().getExtras().getBoolean("isadd");
position = getIntent().getExtras().getInt("position");
if (null != mLogs) {
mbtn_right.setText("修改");
mbtn_right.setTag(BTN_ALTER);
mstrTitle = mLogs.getTitleView();
mstrContent = mLogs.getTextView();
mstrImgList = mLogs.getUrList();
medt_title.setText(mstrTitle);
medt_content.setText(mstrContent);
for (int i = 0; i < 4; i++) {
if (!mstrImgList[i].equals("0")) {
Bitmap bitmap = mGetPicture
.takePictureFormName(mstrImgList[i]);
if (null != bitmap) {
ImageView imageView = (ImageView) findViewById(mintImgId[i]);
imageView.setImageBitmap(bitmap);
}
}
}
medt_title.setEnabled(false);
medt_content.setEnabled(false);
}
}
mbtn_back.setOnClickListener(this);
mbtn_right.setOnClickListener(this);
mbtn_addPicture.setOnClickListener(this);
parentView.setOnTouchListener(this);
medt_content.setOnTouchListener(this);
for (int i = 0; i < mintImgId.length; i++) {
ImageView imageView = (ImageView) findViewById(mintImgId[i]);
imageView.setOnClickListener(this);
imageView.setOnLongClickListener(this);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (0 == requestCode) {
return ;
}
Bitmap bitmap = mGetPicture.getPictureFromResult(resultCode,
requestCode, data);
if (null == bitmap) {
return;
}
String name_R = mGetPicture.getname();
Toast.makeText(this, name_R, Toast.LENGTH_LONG).show();
ImageView imageView = (ImageView) findViewById(mintImgId[mintImgFlat]);
imageView.setImageBitmap(bitmap);
mstrImgList[mintImgFlat] = name_R;
}
private void saveOn() {
mstrTitle = medt_title.getText().toString();
mstrContent = medt_content.getText().toString();
mLogs.setTitleView(mstrTitle);
mLogs.setTextView(mstrContent);
mLogs.setDate(mstrDate);
mLogs.setUrList(mstrImgList);
if (true == isadd) {
Intent intent = getIntent();
intent.putExtra("Logs", mLogs);
Bundle bundle = new Bundle();
EditActivity.this.setResult(0, intent);
} else {
Intent intent = new Intent();
intent.putExtra("Logs", mLogs);
intent.putExtra("position", position);
intent.putExtra("isedit", true);
EditActivity.this.setResult(0, intent);
}
DateSQlite db = new DateSQlite(this);
db.insertLog(mLogs);
EditActivity.this.finish();
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
}
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.ok:
// 判断内容是否为空
if (0 == medt_content.length() && 0 == medt_title.length()) {
Toast.makeText(this, "日记内容还是空的哦!", Toast.LENGTH_SHORT).show();
} else {
// 判断当前内容的状态 编辑模式 or 查看模式
if (mbtn_right.getTag().equals(BTN_ALTER)) {
mbtn_right.setTag(BTN_SAVE);
mbtn_right.setText("保存");
medt_title.setEnabled(true);
medt_content.setEnabled(true);
} else {
saveOn();
}
}
break;
case R.id.back:
if (mbtn_right.getTag().equals(BTN_SAVE)) {
DialogMessegs();
} else {
setResult(1);
EditActivity.this.finish();
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
}
break;
case R.id.addpicture:
mRL_imgGroup.setVisibility(View.VISIBLE);
mintFlat = 1;
break;
case R.id.imageView0:
popuwindow();
mintImgFlat = 0;
break;
case R.id.imageView1:
popuwindow();
mintImgFlat = 1;
break;
case R.id.imageView2:
popuwindow();
mintImgFlat = 2;
break;
case R.id.imageView3:
popuwindow();
mintImgFlat = 3;
break;
case R.id.editcontent:
medt_content.requestFocus();
default:
break;
}
}
public void popuwindow() {
AlertDialog.Builder deleteBuilder = new AlertDialog.Builder(this);
deleteBuilder.setMessage("请选择一个来源");
final int KEY = position;
deleteBuilder.setPositiveButton("相机",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mGetPicture.takePhoto();
}
});
deleteBuilder.setNegativeButton("相册",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mGetPicture.takeAlbum();
}
});
deleteBuilder.create();
deleteBuilder.show();
}
public void DialogMessegs() {
AlertDialog.Builder deleteBuilder = new AlertDialog.Builder(this);
deleteBuilder.setMessage("确定要放弃编辑?");
final int KEY = position;
deleteBuilder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(1);
EditActivity.this.finish();
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
}
});
deleteBuilder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
deleteBuilder.create();
deleteBuilder.show();
}
public void deleteImage(int mintImgFlatx) {
AlertDialog.Builder deleteBuilder = new AlertDialog.Builder(this);
deleteBuilder.setMessage("确定删除图片?");
final int mintImgFlat = mintImgFlatx;
deleteBuilder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ImageView imageView = (ImageView) findViewById(mintImgId[mintImgFlat]);
imageView
.setImageResource(R.drawable.img_add_x);
mstrImgList[mintImgFlat] = 0 + "";
}
});
deleteBuilder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
deleteBuilder.create();
deleteBuilder.show();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
medt_content.requestFocus();
if (1 == mintFlat) {
int height = mRL_imgGroup.getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
mRL_imgGroup.setVisibility(View.GONE);
mintFlat = 0;
}
}
}
return true;
}
@Override
public boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.imageView0:
deleteImage(0);
break;
case R.id.imageView1:
deleteImage(1);
break;
case R.id.imageView2:
deleteImage(2);
break;
case R.id.imageView3:
deleteImage(3);
break;
default:
break;
}
return true;
}
}
4.3.1 MyEditView
package com.wl.android.myeditView;
import com.example.timelinenote.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;
public class MyEditView extends EditText {
// private static final String TAG = "com.lms.todo.views.MyEditView";
private Rect mRect;
private Paint mPaint;
private int mColor = R.color.grayBlown_press;
private final int padding = 10;
private int lineHeight;
private int viewHeight,viewWidth;
public MyEditView(Context context) {
this(context, null);
}
public MyEditView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyEditView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mColor);
mPaint.setStrokeWidth(2);
mPaint.setAntiAlias(true);
setFocusable(true);
setFocusableInTouchMode(true);
}
@Override
protected void onDraw(Canvas canvas) {
int count = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
int lineHeight = 10;
int i = 0;
while (i < count) {
lineHeight = getLineBounds(i, r);
canvas.drawLine(r.left, lineHeight + padding, r.right, lineHeight + padding,
paint);
i++;
}
int maxLines = 15;
int avgHeight = lineHeight / count;
int currentLineHeight = lineHeight;
i = 0;
while(i < maxLines){
currentLineHeight = currentLineHeight + avgHeight + padding;
canvas.drawLine(r.left, currentLineHeight, r.right, currentLineHeight, paint);
i++;
}
super.onDraw(canvas);
}
}
4.3.2 MyImageView
package com.wl.android.myeditView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
int w = this.getWidth();
int h = this.getHeight();
clipPath.addRoundRect(new RectF(0, 0, w, h), 90.0f, 90.0f,
Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
}
4.4.1 DateSQL
package com.wl.android.DateSQLite;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.wl.android.enpty.Logs;
public class DateSQlite {
SQLiteDatabase db;
Context context;
public final static int KEY = 3;
public final static int PASSWORD = 1;
public final static int QUESTION = 2;
public final static int DATE = 0;
public final static int TITLE = 1;
public final static int CONTEXT = 2;
public final static int IMG0 = 3;
public final static int IMG1 = 4;
public final static int IMG2 = 5;
public final static int IMG3 = 6;
public DateSQlite(Context context) {
this.context = context;
db = SQLiteDatabase.openOrCreateDatabase(
context.getCacheDir().toString() + "/my.db3", null);
try {
db.execSQL("create table Logs("
+"date varchar(10) primary key"
+ ", title varchar(50)"
+ ", context varchar(400)"
+ ", img0 varchar(20)"
+ ", img1 varchar(20)"
+ ", img2 varchar(20)"
+ ", img3 varchar(20))");
} catch (Exception e) {
// TODO: handle exception
}
try {
db.execSQL("create table user_table("
+ " id varchar(2) primary key,"
+ " password varchar(8),"
+ " question varchar(200),"
+ " keys varchar(80))"
);
db.execSQL("update user_table"
+ " set password = '0', "
+ " question = '0',"
+ " keys = '0'"
+ " where id = 'Only'");
} catch (Exception e) {
// TODO: handle exception
}
}
public void insertInformation(String password, String question, String keys)
{
try {
inserData(db, question, password, keys);
} catch (Exception e) {
db.execSQL("create table if not exists user_table("
+ " id varchar(2) primary key,"
+ " password varchar(8),"
+ " question varchar(200),"
+ " keys varchar(80))"
);
inserData(db, question, password, keys);
}
}
public void deleteUser(){
deleteImfromation();
}
private void deleteImfromation(){
try {
db.execSQL("update user_table"
+ " set password = '0', "
+ " question = '0',"
+ " keys = '0'"
+ " where id = 'Only'");
} catch (Exception e) {
// TODO: handle exception
}
}
private void inserData(SQLiteDatabase db2, String question, String password, String keys) {
try {
db.execSQL("insert into user_table values(?, ?, ?, ?)"
, new String[]{"Only", password, question,keys});
} catch (Exception e) {
// TODO: handle exception
db.execSQL("update user_table"
+ " set password = '"+ password+"', "
+ " question = '"+question+"',"
+ " keys = '"+keys+"'"
+ " where id = 'Only'");
}
}
public Cursor getInformation()
{
return selectInformation();
}
private Cursor selectInformation()
{
Cursor cursor;
try {
cursor = db.rawQuery("select * from user_table", null);
cursor.moveToFirst();
} catch (Exception e) {
// TODO: handle exception
return null;
}
return cursor;
}
public void deleteLog(Logs logs)
{
db.execSQL("delete from Logs where date = '"+ logs.getDate() +"'");
}
public void deleteLogs()
{
db.execSQL("delete from Logs where date = '*'");
}
public void updataLog(Logs logs)
{
db.execSQL("update Logs"
+ " set title = '"+ logs.getTitleView()+"', "
+ " context = '"+logs.getTextView()+"',"
+ " img0 = '"+logs.getUrList()[0]+"',"
+ " img1 = '"+logs.getUrList()[1]+"',"
+ " img2 = '"+logs.getUrList()[2]+"',"
+ " img3 = '"+logs.getUrList()[3]+"'"
+ " where date = '"+logs.getDate()+"'");
}
public Boolean hasLogs(Logs logs)
{
try {
Cursor cursor;
cursor = db.rawQuery("select * from Logs where date = '"+ logs.getDate()+"'",null);
cursor.close();
return true;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
public ArrayList<Logs> getLogs()
{
Cursor cursor;
ArrayList<Logs> list= new ArrayList<Logs>();
cursor = db.rawQuery("select * from Logs", null);
cursor.moveToFirst();
Boolean haveNext = true;
while (haveNext) {
Logs logs = new Logs();
logs.setDate(cursor.getString(0));
logs.setTitleView(cursor.getString(1));
logs.setTextView(cursor.getString(2));
String string[] = {cursor.getString(3)
, cursor.getString(4)
, cursor.getString(5)
, cursor.getString(6)};
logs.setUrList(string);
list.add(logs);
haveNext = cursor.moveToNext();
}
cursor.close();
return list;
}
public void insertLogs(ArrayList<Logs> list)
{
for (int i = 0; i < list.size(); i++) {
insertLog(list.get(i));
}
}
public void insertLog(Logs logs)
{
String date = logs.getDate();
String title = logs.getTitleView();
String context = logs.getTextView();
String img0 = logs.getUrList()[0];
String img1 = logs.getUrList()[1];
String img2 = logs.getUrList()[2];
String img3 = logs.getUrList()[3];
try {
db.execSQL("insert into Logs values(?, ?, ?, ?, ?, ?, ?)"
, new String []{date
, title
, context
, img0, img1, img2, img3});
} catch (SQLiteConstraintException e) {
e.printStackTrace();
try {
db.execSQL("create table Logs("
+"date varchar(10) primary key"
+ ", title varchar(50)"
+ ", context varchar(400)"
+ ", img0 varchar(20)"
+ ", img1 varchar(20)"
+ ", img2 varchar(20)"
+ ", img3 varchar(20))");
db.execSQL("insert into Logs values(?,?,?,?,?,?,?)"
, new String []
{date
, title
, context
, img0, img1, img2, img3});
} catch (Exception e2) {
System.out.println("日期已存在,内容将被修改");
updataLog(logs);
}
}
}
}
4.5.1 GetPicture
package com.wl.android.enpty;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Path;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.widget.Toast;
public class GetPictureFormCAMER {
private Activity activity;
private static final int CAMEA_TAKE = 1;
private static final int CAMEA_SELECT = 2;
public String name;
private static final String PATH = Environment
.getExternalStorageDirectory() + "/a_Timeline/image";
private boolean isBig = false;
// Environment.getExternalStorageDirectory() +
public GetPictureFormCAMER(Activity activity) {
this.activity = activity;
}
public GetPictureFormCAMER()
{}
public String getname_R() {
new DateFormat();
String name_R = DateFormat.format("yyyyMMMdd_hhmmss",
Calendar.getInstance(Locale.CHINA))
+ ".png";
return name_R;
}
public String getname() {
return name;
}
public String getPath() {
return PATH;
}
public void takeAlbum() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
name = getname_R();
activity.startActivityForResult(intent, CAMEA_SELECT);
}
public void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
new DateFormat();
name = getname_R();
Uri imageUri = Uri.fromFile(new File(PATH, name));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
activity.startActivityForResult(intent, CAMEA_TAKE);
}
public Bitmap getPictureFromResult(int resultCode, int requestCode,
Intent data) {
if (resultCode == activity.RESULT_OK) {
switch (requestCode) {
case CAMEA_TAKE:
Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/" + name);
saveBitmap(name, bitmap);
Toast.makeText(activity, name, Toast.LENGTH_LONG).show();
System.out.println(bitmap.getHeight() + "====="
+ bitmap.getWidth());
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
float scale = bitmap.getWidth() / (float) dm.widthPixels;
Bitmap newBitmap = null;
if (scale > 1) {
newBitmap = zoomBitmap(bitmap, bitmap.getWidth() / scale,
bitmap.getHeight() / scale);
bitmap.recycle();
isBig = true;
}
if (isBig) {
return newBitmap;
} else {
return bitmap;
}
case CAMEA_SELECT:
ContentResolver resolver = activity.getContentResolver();
Uri imgUri = data.getData();
try {
Bitmap photoBitmap = MediaStore.Images.Media.getBitmap(
resolver, imgUri);
saveBitmap(name, photoBitmap);
DisplayMetrics dm_2 = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay()
.getMetrics(dm_2);
float scale_2 = photoBitmap.getWidth()
/ (float) dm_2.widthPixels;
Bitmap newBitmap_2 = null;
if (scale_2 > 1) {
newBitmap_2 = zoomBitmap(photoBitmap,
photoBitmap.getWidth() / scale_2,
photoBitmap.getHeight() / scale_2);
photoBitmap.recycle();
isBig = true;
}
if (isBig) {
return newBitmap_2;
} else {
return photoBitmap;
}
} catch (Exception e) {
// TODO: handle exception
}
break;
default:
break;
}
}
return null;
}
public Bitmap zoomBitmap(Bitmap bitmap, float width, float height) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleheight = ((float) height / h);
matrix.postScale(scaleWidth, scaleheight);
Bitmap newbitBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
true);
return newbitBitmap;
}
public void saveBitmap(String name, Bitmap bm) {
File ff = new File(PATH);
if (ff.exists() == false) {
ff.mkdirs();
}
File f = new File(PATH + "/" + name);
System.out.println("开始保存中.......");
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
System.out.print(" 保存成功 ╮(╯_╰)╭");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("保存失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("保存失败");
}
}
public Bitmap takePictureFormName(String names)
{
Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/" + names);
if (null != bitmap) {
return bitmap;
}
return null;
}
}
4.5.2 Logs
package com.wl.android.enpty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
public class Logs implements Serializable {
private String titleView;
private String textView;
private String tipsView;
private String date;
private String[] urList = {"0","0","0","0"};
public String[] getUrList() {
return urList;
}
public void setUrList(String[] urList) {
this.urList = urList;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTitleView() {
return titleView;
}
public void setTitleView(String titleView) {
this.titleView = titleView;
}
public String getTextView() {
return textView;
}
public void setTextView(String textView) {
this.textView = textView;
}
public String getTipsView() {
return tipsView;
}
public void setTipsView(String tipsView) {
this.tipsView = tipsView;
}
}
4.6.1 MyAdapter
package com.wl.android.myadapter;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.timelinenote.R;
import com.wl.android.enpty.GetPictureFormCAMER;
import com.wl.android.enpty.Logs;
public class Myadapter extends BaseAdapter{
private List<Logs> list;
private Context context;
ViewHolder viewHoder;
private GetPictureFormCAMER getpicture = new GetPictureFormCAMER();
Typeface mTypeface_HM ;
public Myadapter(List<Logs> list, Context context) {
this.list = list;
this.context = context;
mTypeface_HM = Typeface.createFromAsset(context.getAssets(), "fonts/Hym.ttf");
}
public int getCount() {
// TODO 自动生成的方法存根
return list.size();
}
@Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return null;
}
@Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
convertView = View.inflate(context,
R.layout.timeline_index, null);
viewHolder.titleView = (TextView) convertView.findViewById(R.id.title);
viewHolder.textView = (TextView) convertView.findViewById(R.id.text);
viewHolder.dateView = (TextView) convertView.findViewById(R.id.date);
viewHolder.textView.setTypeface(mTypeface_HM);
viewHolder.dateView.setTypeface(mTypeface_HM);
viewHolder.titleView.setTypeface(mTypeface_HM);
viewHolder.img[0] = (ImageView) convertView.findViewById(R.id.imagetlv0);
viewHolder.img[1] = (ImageView) convertView.findViewById(R.id.imagetlv1);
viewHolder.img[2] = (ImageView) convertView.findViewById(R.id.imagetlv2);
viewHolder.img[3] = (ImageView) convertView.findViewById(R.id.imagetlv3);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.titleView.setText(list.get(position).getTitleView());
viewHolder.textView.setText(list.get(position).getTextView());
viewHolder.dateView.setText(list.get(position).getDate());
String names[] = list.get(position).getUrList();
for (int i = 0; i < 4; i++) {
if (!names[i].equals("0")) {
Bitmap bitmap = getpicture.takePictureFormName(names[i]);
if (null != bitmap) {
viewHolder.img[i].setImageBitmap(bitmap);
viewHolder.img[i].setVisibility(View.VISIBLE);
}
}else {
viewHolder.img[i].setVisibility(View.GONE);
}
}
return convertView;
}
}
class ViewHolder{
public TextView titleView;
public TextView textView;
public TextView dateView;
public ImageView[] img = new ImageView[4];
}
----------------------------------我是。。。。的分割线--------------------------------------------------------------------------
突然觉得代码是不是贴太多了囧???不管了 等待更新。。。