-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2329 from WJKAAA/master
#5 实验五+报告
- Loading branch information
Showing
12 changed files
with
530 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../com/example/wjk/edu.hzuapps.androidlabs.soft1614080902135/Soft1614080902135Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.wjk.edu.hzuapps.androidlabs.soft1614080902135; | ||
|
||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
|
||
public class Soft1614080902135Activity extends AppCompatActivity { | ||
|
||
private Button newButton; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_soft1614080902135); | ||
|
||
newButton=(Button)findViewById(R.id.button_1); | ||
newButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
Intent intent=new Intent(Soft1614080902135Activity.this,Soft1614080902135Activity2.class); | ||
startActivity(intent); | ||
} | ||
}); | ||
|
||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
...com/example/wjk/edu.hzuapps.androidlabs.soft1614080902135/Soft1614080902135Activity2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.example.wjk.edu.hzuapps.androidlabs.soft1614080902135; | ||
|
||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Matrix; | ||
import android.graphics.Paint; | ||
import android.net.Uri; | ||
import android.os.Environment; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
import android.widget.Toast; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.OutputStream; | ||
|
||
|
||
public class Soft1614080902135Activity2 extends AppCompatActivity { | ||
private ImageView iv; | ||
private Bitmap baseBitmap; | ||
private Canvas canvas; | ||
private Paint paint; | ||
private Button newButton1; | ||
private Button newButton2; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
|
||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_soft16140809021352); | ||
|
||
|
||
newButton2 = (Button) findViewById(R.id.button_2); | ||
newButton2.setOnClickListener(new View.OnClickListener() { | ||
public void onClick(View v) { | ||
Intent intent1=new Intent(Soft1614080902135Activity2.this,Soft1614080902135Activity3.class); | ||
startActivity(intent1); | ||
} | ||
}); | ||
|
||
|
||
newButton1 = (Button) findViewById(R.id.button_3); | ||
newButton1.setOnClickListener(new View.OnClickListener() { | ||
public void onClick(View v) { | ||
save(); | ||
} | ||
}); | ||
|
||
|
||
|
||
this.iv = (ImageView) this.findViewById(R.id.iv); | ||
// 创建一张空白图片 | ||
baseBitmap = Bitmap.createBitmap(1200, 1600, Bitmap.Config.ARGB_8888); | ||
// 创建一张画布 | ||
canvas = new Canvas(baseBitmap); | ||
// 画布背景为白色 | ||
canvas.drawColor(Color.WHITE); | ||
// 创建画笔 | ||
paint = new Paint(); | ||
// 画笔颜色为黑色 | ||
paint.setColor(Color.BLACK); | ||
// 宽度5个像素 | ||
paint.setStrokeWidth(10); | ||
// 先将白色背景画上 | ||
canvas.drawBitmap(baseBitmap, new Matrix(), paint); | ||
iv.setImageBitmap(baseBitmap); | ||
|
||
iv.setOnTouchListener(new View.OnTouchListener() { | ||
int startX; | ||
int startY; | ||
|
||
@Override | ||
public boolean onTouch(View v, MotionEvent event) { | ||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
// 获取手按下时的坐标 | ||
startX = (int) event.getX(); | ||
startY = (int) event.getY(); | ||
break; | ||
case MotionEvent.ACTION_MOVE: | ||
// 获取手移动后的坐标 | ||
int stopX = (int) event.getX(); | ||
int stopY = (int) event.getY(); | ||
// 在开始和结束坐标间画一条线 | ||
canvas.drawLine(startX, startY, stopX, stopY, paint); | ||
// 实时更新开始坐标 | ||
startX = (int) event.getX(); | ||
startY = (int) event.getY(); | ||
iv.setImageBitmap(baseBitmap); | ||
break; | ||
} | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
public void save() { | ||
try { | ||
File file = new File(Environment.getExternalStorageDirectory(), | ||
System.currentTimeMillis() + ".jpg"); | ||
OutputStream stream = new FileOutputStream(file); | ||
baseBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); | ||
stream.close(); | ||
Intent intent = new Intent(); | ||
intent.setAction(Intent.ACTION_MEDIA_MOUNTED); | ||
intent.setData(Uri.fromFile(Environment | ||
.getExternalStorageDirectory())); | ||
sendBroadcast(intent); | ||
} catch (Exception e) { | ||
Toast.makeText(Soft1614080902135Activity2.this, "保存图片成功", Toast.LENGTH_SHORT).show(); | ||
|
||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
97 changes: 97 additions & 0 deletions
97
...com/example/wjk/edu.hzuapps.androidlabs.soft1614080902135/Soft1614080902135Activity3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.example.wjk.edu.hzuapps.androidlabs.soft1614080902135; | ||
|
||
import android.os.Bundle; | ||
import android.support.design.widget.FloatingActionButton; | ||
import android.support.design.widget.Snackbar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.Toolbar; | ||
import android.view.View; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Bundle; | ||
import android.os.Environment; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.ImageView; | ||
|
||
public class Soft1614080902135Activity3 extends AppCompatActivity { | ||
private EditText editText; | ||
private Button button; | ||
private ImageView imageView; | ||
private Bitmap bitmap; | ||
|
||
Handler h; | ||
|
||
{ | ||
h= new Handler() { | ||
public void handleMessage(Message msg) { | ||
if (msg.what == 111) { | ||
imageView.setImageBitmap(bitmap); | ||
} | ||
} | ||
|
||
; | ||
}; | ||
} | ||
|
||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_soft16140809021353); | ||
editText = (EditText) findViewById(R.id.imagepath); | ||
button = (Button) findViewById(R.id.upload); | ||
imageView = (ImageView) findViewById(R.id.imageView); | ||
button.setOnClickListener(new OnClickListener() { | ||
public void onClick(View arg0) { | ||
new Thread(t).start(); | ||
} | ||
}); | ||
} | ||
|
||
//为了下载图片资源,开辟一个新的子线程 | ||
Thread t=new Thread(){ | ||
public void run() { | ||
//下载图片的路径 | ||
String iPath=editText.getText().toString(); | ||
try { | ||
//对资源链接 | ||
URL url=new URL(iPath); | ||
//打开输入流 | ||
InputStream inputStream=url.openStream(); | ||
//对网上资源进行下载转换位图图片 | ||
bitmap=BitmapFactory.decodeStream(inputStream); | ||
h.sendEmptyMessage(111); | ||
inputStream.close(); | ||
|
||
//再一次打开 | ||
inputStream=url.openStream(); | ||
File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/"); | ||
FileOutputStream fileOutputStream=new FileOutputStream(file); | ||
int hasRead=0; | ||
while((hasRead=inputStream.read())!=-1){ | ||
fileOutputStream.write(hasRead); | ||
} | ||
fileOutputStream.close(); | ||
inputStream.close(); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}; | ||
}; | ||
|
||
|
||
} |
Oops, something went wrong.