Beginner Android, always wanted to do a comic browser, but do not know who controls simply do it yourself a try
Function as follows:
1 Double-click to shrink or enlarge images, support for full-screen and 1:1 playback
2 Tap the screen to the left and right sides of the support page
3. 1:1 preview
supports drag amplified4 reads zip package, dynamic loading
Provided to the students to learn together android beginner, expert criticism
package com.marssoft.imagetest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
/ **
* Cartoon shows the main class.
*
* @ Author Mars.CN
*
* /
public class CartoonView extends View {
public static final String PACKAGE_TYPE_ZIP = "ZIP"; / / zip bag
public static final String PACKAGE_TYPE_MCP = "MCP"; / / special packages
public static final int PAGE_AREA_SIZE = 40; / / flip region
private Context mContext = null;
private ArrayListpics = null; / / zip package file list
private ZipFile zipFile = null; / / comics package file
private int playIndex = 0; / / is playing in the picture.
private Bitmap playBitmap = null; / / are playing in the picture
private Bitmap nextBitmap = null; / / next picture
private Bitmap lastBitmap = null; / / on a picture
private Paint mPaint = new Paint (); / / Global brush
private float zoom = -1; / / zoom level, currently only supports full-screen playback with two formats
by sizeprivate int px = 0, py = 0; / / drawing position
private String cartoonPackagePath = ""; / / path (usually sdcard / cmsreader / cartoon / location)
picture packageprivate String packageType = ""; / / Picture Package Type
private int width, height; / / canvas size
private _ActionEvent actionEvent = null; / / currently operating in the Event, double-click the event may be used in which the coordinates and other information
@ Override
protected void onDraw (Canvas canvas) {
/ / System.out.println ("start redraw");
super.onDraw (canvas);
Bitmap p = getBitmap ();
canvas.drawBitmap (p, 0, 0, mPaint);
/ / System.out.println ("Paint successful");
}
/ **
* The main class constructor reader
*
* @ Param context
* @ Param cpkPath
* /
public CartoonView (Context context, String cpkPath) {
super (context);
mContext = context;
setCartoonPackagePath (cpkPath);
Activity act = (Activity) context;
width = act.getWindowManager () getDefaultDisplay () getWidth ();..
height = act.getWindowManager () getDefaultDisplay () getHeight ();..
registerDoubleClickListener ();
}
/ **
* Registered a double-click event
* /
private void registerDoubleClickListener () {
setOnClickListener (new OnClickListener () {
private static final int DOUBLE_CLICK_TIME = 350; / / double click interval 350 msec
private boolean waitDouble = true; / / Wait double click
@ Override
public void onClick (View v) {
if (waitDouble) {
waitDouble = false; double-click the event
/ / executionnew Thread () {
public void run () {
try {
sleep (DOUBLE_CLICK_TIME); / / wait for the double-click time, or perform a click event
if (! waitDouble) {
/ / If after waiting for an event or double-click on the pre-execution state is considered to click
waitDouble = true;
onSingleClick ();
}
} Catch (InterruptedException e) {
e.printStackTrace ();
}
}
.} Start ();
} Else {
waitDouble = true;
onDoubleClick (); / / execute double click
}
}
});
}
/ **
* Double-click an event to trigger
* /
private void onDoubleClick () {
/ / System.out.println ("Double-click the image coordinates:" + actionEvent.getX () + "," + actionEvent.getY ());
zoomPicture ();
}
/ **
* Adjust the picture size.
* If the zoom is less than 1, the zoom size to 1, if the zoom size is equal to 1, then scaled to full screen size.
* /
private void zoomPicture () {
if (zoom <1) {
zoom = 1;
/ *
* If it is full-screen playback, already double-click to zoom position as the center, as follows:
* Click on the actual position is determined: (1) to calculate the original scale, two coordinate / get the actual location of the original proportions
.* This position to the center of the screen, and re-calculate a new XY coordinate position
* /
float fzoom = 0;
fzoom = (float) height / playBitmap.getHeight ();
if (playBitmap.getWidth () * fzoom> width) {
fzoom = (float) width / playBitmap.getWidth ();
}
float fx = actionEvent.getX () / fzoom;
float fy = actionEvent.getY () / fzoom;
px = (int) ((width) / 2-fx);
py = (int) ((height) / 2-fy);
moveXY (0,0);
} Else {
zoom = -1;
}
invalidate (); / / refresh Canvas
}
/ **
* Click on the trigger time
* /
private void onSingleClick () {
/ / System.out.println ("click the picture" + actionEvent.getX () + "," + actionEvent.getY ());
/ / If you click on that, and the zoom is not one, it is determined whether the flip trigger area
if (zoom! = 1) {
if (actionEvent.getX () <= PAGE_AREA_SIZE) {
/ / Indicates the opposite Previous
playIndex -;
if (playIndex <0) {
playIndex = 0;
/ / Toast.makeText (mContext, "there is no content in front of", Toast.LENGTH_SHORT) show ();.
}
} Else if (actionEvent.getX ()> = width-PAGE_AREA_SIZE) {
playIndex + +;
if (playIndex> = pics.size ()) {
playIndex = pics.size () -1;
/ / Toast.makeText (mContext, "no content behind a", Toast.LENGTH_SHORT) show ();.
}
}
playBitmap = createBitmap (playIndex);
/ / Invalidate () ;/ / redraw
postInvalidate (); / / must call an external redraw, otherwise execution error
}
}
/ **
* OnTouchFevent event
reconstruction of the parent class* /
@ Override
public boolean onTouchEvent (MotionEvent event) {
/ / Pressed when the event saved, and then double-click the event may be used in the coordinate values
switch (event.getAction ()) {
case MotionEvent.ACTION_DOWN: / / Press event
break;
case MotionEvent.ACTION_MOVE: / / drag event
/ / System.out.println ("ACTION_MOVE:" + event.getX () + "," + event.getY ());
/ / Determine whether or equal to 1, if equal to 1, then the full-screen playback, you can move, otherwise flip
if (zoom == 1) {
if (actionEvent! = null && actionEvent.getAction () == MotionEvent.ACTION_MOVE) {
/ / If the last action is moving, you can move the picture
moveXY (event.getX ()-actionEvent.getX (), event.getY ()-actionEvent.getY ());
invalidate ();
}
} Else {
/ / Flip, allowing only move around
}
break;
}
actionEvent = new _ActionEvent (event);
return super.onTouchEvent (event);
}
/ **
* Moving Pictures
* @ Param x
* @ Param y
* /
private void moveXY (float x, float y) {
px + = x;
Are / / Determine x over left and right margins
/ / Px = px?px = px px = px> 0 0: px;
?py + = y;
py = pypy = py> 0 0: py;
?/ / Determine y exceeds the upper and lower boundaries
}
/ **
* Reconstruction of the parent class constructor.
*
* @ Param context
* /
public CartoonView (Context context) {
this (context, "");
}
/ **
* Initialize the picture package.
* /
public void initPackage () {
if (getPackageType (). equals (PACKAGE_TYPE_MCP)) {
/ / If the file mcp format, then transferred directly to zip format to read
} Else if (getPackageType (). Equals (PACKAGE_TYPE_ZIP)) {
/ / If the zip file format, generated directly
try {
zipFile = new ZipFile (getCartoonPackagePath ());
} Catch (IOException e) {
e.printStackTrace ();
}
}
Enumeratione = (Enumeration ) zipFile.entries (); / / get zip file directory and file list
ZipEntry entry = null;
pics = new ArrayList();
while (e.hasMoreElements ()) {
entry = e.nextElement ();
if (! entry.isDirectory ()) {
/ / If the file is not a directory, then add to the list
pics.add (entry);
}
}
/ / Initialize the current playback image
playBitmap = createBitmap (playIndex);
}
/ **
* Get the picture package path.
*
* @ Return
* /
public String getCartoonPackagePath () {
return cartoonPackagePath;
}
/ **
* Set picture package path.
*
* @ Param cartoonPackagePath
* /
public void setCartoonPackagePath (String cartoonPackagePath) {
this.cartoonPackagePath = cartoonPackagePath;
/ / Set the type
if (cartoonPackagePath.endsWith (". mcp")) {
setPackageType (PACKAGE_TYPE_MCP);
} Else {
setPackageType (PACKAGE_TYPE_ZIP);
}
initPackage ();
}
/ **
* Get the Picture Package format.
*
* @ Return
* /
public String getPackageType () {
return packageType;
}
/ **
* Set picture package format.
*
* @ Param packageType
* /
private void setPackageType (String packageType) {
this.packageType = packageType;
}
/ **
* According to the picture ID to create a picture, numbered from 0.
*
* @ Param pictureid
* @ Return
* /
private Bitmap createBitmap (int pictureid) {
/ / System.out.println ("CreateBitmapBegin");
Bitmap result = null;
try {
result = new BitmapDrawable (zipFile.getInputStream (pics
. Get (pictureid))) getBitmap ();.
} Catch (IOException e) {
e.printStackTrace ();
}
/ / System.out.println ("CreateBitmapEnd");
return result;
}
/ **
* Create cache picture sequence to improve the refresh rate.
* The lastBitmap to nextBitmap a teleport, and finally determine whether playBitmap is empty, if empty, create a current image sequence.
* When this method is called, has been re-assignment of confirmation playIndex too.
* @ Para index 1 represents moving backwards, -1 means to move forward.
* /
/ / Private synchronized void displayPictures (short index) {
/ /
/ /}
/ **
* Create a picture in a picture on the screen.
*
* @ Return
* /
private Bitmap getBitmap () {
Bitmap result = Bitmap.createBitmap (width, height, Config.ARGB_8888);
Canvas canvas = new Canvas (result);
canvas.drawColor (Color.BLACK);
/ / Draw pictures in the current playback.
if (zoom <1) {
/ / If it is full-screen mode
/ *
* 1. Scaling high, get scaling 2. Width multiplied by the scale, if the ratio of the actual width of the resulting screen width, the width is calculated by scaling
* 3. Been drawn proportional to the height and width, and draw pictures
* /
zoom = (float) height / playBitmap.getHeight ();
if (playBitmap.getWidth () * zoom> width) {
zoom = (float) width / playBitmap.getWidth ();
}
int c_width = (int) (playBitmap.getWidth () * zoom);
int c_height = (int) (playBitmap.getHeight () * zoom);
/ / Calculate the drawing position
px = (width - c_width) / 2;
py = (height - c_height) / 2;
Matrix matrix = new Matrix ();
matrix.postScale (zoom, zoom);
/ / Scale and draw pictures
canvas.drawBitmap (Bitmap.createBitmap (playBitmap, 0, 0, playBitmap
. GetWidth (), playBitmap.getHeight (), matrix, true), px, py,
mPaint);
} Else {
/ / Draw
/ / System.out.println (px + "," + py);
canvas.drawBitmap (playBitmap, px, py, mPaint);
}
return result;
}
/ **
* Internal Action.
* @ Author Wang Chong
*
* /
private class _ActionEvent {
private float x, y;
private int action;
public float getX () {
return x;
}
public void setX (float x) {
this.x = x;
}
public float getY () {
return y;
}
public void setY (float y) {
this.y = y;
}
public int getAction () {
return action;
}
public void setAction (int action) {
this.action = action;
}
public _ActionEvent (MotionEvent event) {
setX (event.getX ());
setY (event.getY ());
setAction (event.getAction ());
}
}
}
zip package can be placed on the sd card, or change their position.
Just provide learning, I hope you have a better way to improve everyone posted together experts mistakenly accused<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Wood was watching?
Reply:
Reply:
Cattle. . Thanks, trial
Reply:
Read the zip file Times java.util.zip.ZipException: too short to be Zip abnormal ask is do you mean?
Reply:
Well, our company is also doing this, the first top
Reply:
Good thing ah, support support ~
Reply:
The landlord can not give me a source, thank you, changingshow@163.com
Reply:
Are you sure you are buddy child android beginner? ?
Reply:
The landlord can not give me a source, thank you, changingshow@163.com
Reply:
The landlord can not give me a source, thank you, 1353545432@qq.com
Reply:
Find the source ah 32192373@qq.com
Reply:
Landlord, if I want to read compressed within the specified file, how to do it
No comments:
Post a Comment