Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular matching Pictures in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use regular matching pictures in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Xml

Activity

Control, I put all the pictures in the phone's memory, all of which must be read first.

Get permissions in the AndroidManifest.xml file

Package com.example.mygesture;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Environment;import android.os.PatternMatcher;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.MotionEvent;import android.widget.ImageView;import android.widget.Toast;import android.widget.ViewFlipper;import java.io.InputStream;import java.util.LinkedList;import java.util.List;import java.util.regex.Matcher Import java.util.regex.Pattern;public class MainActivity extends AppCompatActivity {private ViewFlipper viewFlipper; private List list=new LinkedList (); private GestureDetector jc; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / fetch control viewFlipper = (ViewFlipper) findViewById (R.id.vf_main_one) / / determine whether if (Environment.getExternalStorageState (). Equals (Environment.MEDIA_MOUNTED)) {/ / fetch the absolute path of the memory card String abspath=Environment.getExternalStorageDirectory () .getAbsolutePath (); / / create the file object File file=new File (abspath); / / fetch all files and folders in the memory card [] filetwo=file.listFiles () For (File file1: filetwo) {try {/ / calls the recursive method TraversalSearch (file1) of the file;} catch (IOException e) {e.printStackTrace () The pictures in the collection are all pictures in the memory card for (ImageView imageView: list) {/ / add all pictures in the collection to the (ViewFlipper) control viewFlipper.addView (imageView) } / / set up gesture detector. Parameter 1: detection event jc of context parameter 2 detector jc = new GestureDetector (this,new GestureDetector.OnGestureListener () {@ Override public boolean onDown (MotionEvent e) {/ / trigger return false } @ Override public void onShowPress (MotionEvent e) {} @ Override public boolean onSingleTapUp (MotionEvent e) {/ / tap trigger return false;} @ Override public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {/ / Mobile trigger return false } @ Override public void onLongPress (MotionEvent e) {/ / often press trigger} @ Override / / after sliding, trigger public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {/ / this method, parameter 1: start position parameter 2: end position Parameter 3-4: X-axis y-axis if (e2.getX ()-e1.getX () > 200) {/ / display the previous viewFlipper.showPrevious () Toast.makeText (MainActivity.this, Toast.LENGTH_SHORT). Show ();} if (e1.getX ()-e2.getX () > 200) {/ / display the next viewFlipper.showNext (); Toast.makeText (MainActivity.this, "004", Toast.LENGTH_SHORT). Show ();} return false;}}) } @ Override / / rewrite the touch callback function public boolean onTouchEvent (MotionEvent event) {/ / set the touch function to the gesture detector, must write! Return jc.onTouchEvent (event);} / / query the recursive method public void TraversalSearch (File file) throws IOException {if (file.isDirectory ()) {File [] filetwo=file.listFiles (); for (File file1: filetwo) {TraversalSearch (file1);}} else {String name=file.getName () / / Image format String regStr = "[Gg] [Ii] [Ff] | [Jj] [Pp] [Gg] | [Bb] [Mm] [Pp] | [Jj] [Pp] [Ee] [Gg]"; / / put the regular matching format into the class Pattern pattern = Pattern.compile (regStr); / / put the matching format into Matcher matcher = pattern.matcher (name) / / matching result, which matches true and false if (matcher.find ()) {ImageView image=new ImageView (this); / / gets the file Bitmap bitmap= BitmapFactory.decodeFile (file.getAbsolutePath (). Trim ()) through the drawing factory and uses the file path; / / sets the picture file into the picture control image.setImageBitmap (bitmap); if (list.size ())

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report