自定义波浪线,待改进

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_15197693/article/details/79973930

基本思路:贝塞尔三阶曲线

public class WaveView extends View {

    private float mCenterX;
    private float mCenterY;
    private float startX;
    private float startY;
    private float endX;
    private float endY;
    private float controlX1;
    private float controlX2;
    private float controlY1;
    private float controlY2;

    private float wave1;
    private float wave2;

    private Paint paint;
    private Paint arcPaint;

    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "wave", 0, 3000);
        animator.setDuration(4000);
        animator.setRepeatMode(ValueAnimator.INFINITE);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();

        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);

        arcPaint = new Paint();
        arcPaint.setStyle(Paint.Style.FILL);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);
        arcPaint.setStrokeWidth(10);
    }

    public float getWave() {
        return wave1;
    }

    public void setWave(float wave) {
        this.wave1 = wave;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mCenterX = canvas.getWidth() / 2;
        mCenterY = canvas.getHeight() / 2;
        startX = 0;
        startY = mCenterY;
        endX = mCenterX * 2;
        endY = mCenterY;

        controlX2 = mCenterX + wave1 > mCenterX * 2 ? (mCenterX + wave1) % canvas.getWidth() : mCenterX + wave1;
        controlY2 = mCenterY + 100;
        controlX1 = controlX2 - 300;
        controlY1 = mCenterY - 100;

        canvas.drawPoint(mCenterX, mCenterY, arcPaint);
        canvas.drawPoint(controlX1, controlY1, arcPaint);
        canvas.drawPoint(controlX2, controlY2, arcPaint);
        Path path = new Path();
        path.moveTo(startX, mCenterY);
        path.cubicTo(controlX1, controlY1, controlX2, controlY2, endX, mCenterY);
        canvas.drawPath(path, paint);

    }
}

阅读更多

Lucence索引创建 [待改进]

10-19

[img=http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/15.gif][/img]rnrnimport java.io.BufferedReader;rnimport java.io.File;rnimport java.io.FileInputStream;rnimport java.io.IOException;rnimport java.io.InputStreamReader;rnrnimport org.apache.lucene.analysis.Analyzer;rnimport org.apache.lucene.analysis.standard.StandardAnalyzer;rnimport org.apache.lucene.document.Document;rnimport org.apache.lucene.document.Field;rnimport org.apache.lucene.document.LongField;rnimport org.apache.lucene.document.StringField;rnimport org.apache.lucene.document.TextField;rnimport org.apache.lucene.document.Field.Store;rnimport org.apache.lucene.index.IndexWriter;rnimport org.apache.lucene.index.IndexWriterConfig;rnimport org.apache.lucene.index.Term;rnimport org.apache.lucene.index.IndexWriterConfig.OpenMode;rnimport org.apache.lucene.store.Directory;rnimport org.apache.lucene.store.FSDirectory;rnimport org.apache.lucene.util.Version;rnrnpublic class IndexerHelper rnrn public enum CreateMode rn CREATE, UPDATErn rnrn /**rn * 描述:生成索引文件rn * rn * @param args 命令行指令rn * "-index/-i",以下为索引存储目录rn * "-source/-s",以下为索引源目录rn * "-update/-u",更新操作 rn * @return Usage: java ConstructIndexer [-index/-i] "c:\index" [-source/s] "c:\source" [[-update/-u] / isNull]rn * @see java.lang.String.equalsIgnoreCase()rn * @throws IOExceptionrn */rn public static void create(String[] args) rn if (args == null || args.length >= 4) rn System.out.println("Usage:java ConstructIndexer [-index/-i] \"c:\\index\" [-source/s] \"c:\\source\" [[-update/-u] / isNull]");rn System.exit(1);rn rn String iDir = null, sDir = null;rn boolean update = false;rn for (int a = 0; a < args.length; a++) rn if ("-index".equalsIgnoreCase(args[a])) rn iDir = args[a + 1];rn rn else if ("-source".equalsIgnoreCase(args[a])) rn sDir = args[a + 1];rn rn else if ("-update".equalsIgnoreCase(args[a])) rn update = true;rn rn rn File idx = new File(iDir);rn if (!idx.exists() || !idx.isDirectory()) rn System.out.println("Index directory is not exist.");rn System.exit(1);// by exception interruptrn rn Directory directory;rn try rn directory = FSDirectory.open(idx);rn // open the index directoryrn Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);rn IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);rn config.setOpenMode(update ? OpenMode.CREATE_OR_APPEND : OpenMode.CREATE);rn IndexWriter writer = new IndexWriter(directory, config);rn addDocs(writer, sDir);rn writer.commit();rn writer.close();rn rn catch (IOException e) rn e.printStackTrace();rn rn rnrn public static void create(String iDir, String sDir, CreateMode mode) rn if (iDir == null || sDir == null || "".equals(iDir.trim()) || "".equals(sDir.trim())) rn return;rn rn File idx = new File(iDir);rn if (!idx.exists() || !idx.isDirectory()) rn System.out.println("Index directory is not exist.");rn rn Directory directory;rn try rn directory = FSDirectory.open(idx);rn // open the index directoryrn Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);rn IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);rn IndexWriter writer = new IndexWriter(directory, config);rn addDocs(writer, sDir);rn writer.commit();rn writer.close();rn rn catch (IOException e) rn e.printStackTrace();rn rn rnrn public static void addDocs(IndexWriter writer, String sDir) rn File source = new File(sDir);rn if (!source.exists()) rn System.out.println("Source directory is not exist.");rn rn File[] files = source.listFiles();rn if (files.length > 0) rn int f = 0;rn for (;;) rn if (files[f].isDirectory()) rn addDocs(writer, sDir);rn rn else rn if (files[f].getName().toLowerCase().endsWith(".txt"))rn try rn addFileDocs(writer, files[f]);rn rn catch (IOException e) rn e.printStackTrace();rn rn rn f++;rn if (f == files.length) rn break;rn rn rn rn rnrn public static void addFileDocs(IndexWriter writer, File sFile) throws IOException rn if (!sFile.canRead()) rn System.out.println(sFile.getCanonicalPath() + " can not Read.");rn rn Document document = new Document();rn FileInputStream fis = new FileInputStream(sFile);rn Field field = new StringField("path", sFile.getPath(), Store.YES);rn document.add(field);rn field = new LongField("lastModified", sFile.lastModified(), Store.YES);rn document.add(field);rn field = new TextField("contents", new BufferedReader(new InputStreamReader(fis, "utf-8")));rn document.add(field);rn OpenMode mode = writer.getConfig().getOpenMode();rn if (mode == OpenMode.CREATE) rn // Addrn System.out.println("Create index:" + sFile.getPath());rn writer.addDocument(document);rn rn else if (mode == OpenMode.CREATE_OR_APPEND) rn // Updatern System.out.println("Update index:" + sFile.getPath());rn writer.updateDocument(new Term("filePath", sFile.getPath()), document);rn rn fis.close();rn rnrn public static void main(String[] args) rn if (args != null && args.length > 3) rn create(args);rn rn else rnrn create("f:/test/idx", "f:/test/source", CreateMode.UPDATE);rnrn rn rnrnjars:lucene-core-4.0.0.jar,lucene-analyzers-common-4.0.0.jarrn1.update时仍重复创建索引问题?rn

没有更多推荐了,返回首页